I created a small program to download a zip file from a direct link and after that all the contents are in the same directory are derived . It does not download anything and does not even remove it is what I have done so far:
package main; Import java.io.buffferedOutputStream; Import java.io.file; Import java.io.FileInputStream; Import java.io.FileOutputStream; Import java.io.IOException; Import java.net.URL; Import java.nio.channels.Channels; Import java.nio.channels.ReadableByteChannel; Import java.util.zip.ZipEntry; Import java.util.zip.ZipInputStream; Public class main {static string url = "https://www.dropbox.com/s/uml938guklfvo7r/Tekst.zip?dl=1"; Static int lastSlashIndex = url.lastIndexOf ('/'); Static string file name = url.substring (last slash index + 1, url.length () - 5); Fixed string filepath = "C:"; Private Static Final Int buffer = 4096; Public static zero main (string [] args) {try {URL website = new URL (url); Readable child channel RBC; RBC = Channel. New Channel (website.openstream ()); New file (filepath + filename) .createNewFile (); FileOutputStream fos = New FileOutputStream (file path + filename); Fos.getChannel () Transferfram (RBC, 0, long.mx_avara); Fos.close (); } Hold (exception e) {e.printStackTrace (); } Try {unzip (file path + filename, file path); } Hold (IOException e) {e.printStackTrace (); }} Public static zero unzip (string zip file path, string distance directory) throws IOException {file destDir = new file (destDirectory); If (DestDir.exists ()) {destDir.mkdir (); } ZIP Intstream ZIPine = new zip input page (new file inputstreet (zip file path)); Zip entry = zipin.taginentry (); // Repeats with entries in the zip file (entry! = Null) {string filePath = destDirectory + File.Sseparator + entry.getName (); If (! Entry.isDirectory ()) {// If the entry is a file, then it removes the file (zipin, file path); } Else {// If the entry is a directory, then create the directory file directory = new file (file path); Dir.mkdir (); } ZipIn.closeEntry (); Entry = Zip InJetanExcantry (); } ZipIn.close (); } Private static zero extracting file (zip inputstream zipin, string filepath) throws IOException {line 68 & gt; & Gt; BufferedOutputStream Boss = New BufferedOutputStream (New FileOutputStream (filePath)); Byte [] bytes = new byte [buffer]; Int read = 0; While ((read = zipIn.read (bytesIn))! = -1) {bos.write (bytesIn, 0, read); } boss. Close (); }}
Here is an error:
java.io.FileNotFoundException: c: \ Tekst.txt (entry denied) is java.io. FileOutputStream.open (Basic method) on Java.io.FileOutputStream. & Lt; Init & gt; (Unknown source) at java.io.FileOutputStream & Lt; Init & gt; (Unknown sources) main. Main. Chief on ExtactFile (Main .Java: 68). Main. Main on Main (Main.Java 55) Main.
Apparently this file has just been created yet. Has not gone. You can make it with
new file (yourFilepath) .createNewFile ()
and before you Required Call
File Optical Stream fos = New FileOutputStream (File Path + File Name);
which throws an exception, then it works perfectly well.
No comments:
Post a Comment