Results 1 to 17 of 17
Thread: Unzip files and folders
- 02-04-2012, 01:30 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Unzip files and folders
I have serach though the whole web and I have only got errors.
My zip file has some folders in it for example "folder1" and "folder2". And with all the codes I have tried, I get this error:
Cant find path to /xxxxxxxx/folder1
Or something like that.
In fact none of the codes I have tried have extracted anything at all.
This is the last one I tried, but the other ones are very similar:
Java Code:BUFFER = 2048; File inFile = new File(ZipName); File outFolder = new File("/" + ZipName); try { BufferedOutputStream out = null; ZipInputStream in = new ZipInputStream( new BufferedInputStream( new FileInputStream(inFile))); ZipEntry entry; while((entry = in.getNextEntry()) != null) { //System.out.println("Extracting: " + entry); int count; byte data[] = new byte[BUFFER]; // write the files to the disk out = new BufferedOutputStream( new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER); while ((count = in.read(data,0,BUFFER)) != -1) { out.write(data,0,count); } out.flush(); out.close(); } in.close(); //return outFolder; } catch(Exception e) { e.printStackTrace(); //return inFile; }
- 02-04-2012, 01:48 AM #2
Re: Unzip files and folders
Can you post the compete text of the error message?Or something like that.
- 02-04-2012, 05:41 AM #3
Re: Unzip files and folders
... along with a SSCCE (Short, Self Contained, Compilable and Executable) example that clearly demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-04-2012, 05:30 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Unzip files and folders
I am new in programming, so I dont understand what a SSCCE means, so I have made what I could:Java Code:java.io.FileNotFoundException: \last.zip\Prueba1.txt (El sistema no puede encontrar la ruta especificada) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at voxel.updater.extract(updater.java:237) at voxel.updater.download(updater.java:195) at voxel.updater.updater(updater.java:281) at voxel.Window.main(Window.java:10)
and here is the zip file I am using: last.zipJava Code:package com.unzip; //Your Package import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Unzip { //Your Class Name private static String ZipName = "last.zip"; //Your Zip File To Be Extracted private static final int BUFFER = 2048; public static void main(String[] args) { File inFile = new File(ZipName); File outFolder = new File("/" + ZipName); try { BufferedOutputStream out = null; ZipInputStream in = new ZipInputStream( new BufferedInputStream( new FileInputStream(inFile))); ZipEntry entry; while((entry = in.getNextEntry()) != null) { int count; byte data[] = new byte[BUFFER]; out = new BufferedOutputStream( new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER); while ((count = in.read(data,0,BUFFER)) != -1) { out.write(data,0,count); } out.flush(); out.close(); } in.close(); } catch(Exception e) { e.printStackTrace(); } } }
-
Re: Unzip files and folders
- 02-04-2012, 05:42 PM #6
Re: Unzip files and folders
Does the folder in the error message exist: \last.zip\java.io.FileNotFoundException: \last.zip\Prueba1.txt
Where are the packages shown in the error message: voxel.updater and voxel.Window ?
Your posted code is NOT the same code as generated the error message.
You need to have the error message and the code agree.Last edited by Norm; 02-04-2012 at 05:44 PM.
- 02-04-2012, 07:52 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Unzip files and folders
I am not english, thats why I didn't undertand SSCCE article.
I think my error was that I was saying that I wanted the zip file to be extracted to a folder called "last.zip" but what I really want is to extract the zip file CALLED "last.zip" to the root folder of my program.
So my question now is:
How can I set the folder to the root folder?
The reason package dont match is that I have my class updater which is in a package voxel (the name of my program) cannot run by itself, so I made another class exactly the same but which can run by itself, so you could copy paste and run it easily (thats the only thing I think I have understood from SSCCEE Article). Finally my class Window has nothing to do with the unzipping, so I have just ommited it as I have read in SSCCE Article.
Sorry about my mistakes.
-
Re: Unzip files and folders
I'm no expert in this, but perhaps you need to make directories when one is needed or encountered. For example,
Java Code:import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Unzip2 { private static final String IN_PATH = "C:/Users/Pete/Documents/Fubar/"; private static final String ZIP_NAME = "last.zip"; private static final String OUT_PATH = "out/"; public static void main(String[] args) { extract(IN_PATH + ZIP_NAME, IN_PATH + OUT_PATH); } private static void extract(String zipPath, String outDirPath) { File outDir = new File(outDirPath); if (!outDir.exists()) { // if output directory doesn't exist outDir.mkdir(); // make it } BufferedInputStream bis = null; BufferedOutputStream bos = null; byte[] buffer = new byte[2^16]; try { FileInputStream fis = new FileInputStream(zipPath); ZipInputStream zis = new ZipInputStream(fis); bis = new BufferedInputStream(zis); ZipEntry ze = null; while ((ze = zis.getNextEntry()) != null) { if (ze.isDirectory()) { System.out.println("directory: " + ze.getName()); File dir = new File(outDirPath + ze.getName()); if (!dir.exists()) { // if directory doesn't exist dir.mkdir(); // make it! } } else { System.out.println("file: " + ze.getName()); try { File file = new File(outDirPath + ze.getName()); FileOutputStream fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); while (bis.read(buffer) > 0) { bos.write(buffer); } } finally { if (bos != null) { bos.close(); } } } zis.closeEntry(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
- 02-04-2012, 09:36 PM #9
Re: Unzip files and folders
If you are having problems getting your program to write to the desired directory, make a small test program that writes one line to a file. Then test different paths with that program until it writes to the directory you want.
When you find the correct path, copy the path into the unzip program.
- 02-04-2012, 09:54 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Unzip files and folders
Ok that was ONE of my problems, not the problem is to extract folders, I get errors saying folders dont exixst, it should create thim if that happens?
- 02-04-2012, 09:58 PM #11
Re: Unzip files and folders
The File class has a method for creating folders.I get errors saying folders dont exist
- 02-05-2012, 12:31 AM #12
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Unzip files and folders
and how can I indicate the program to extract the files in the same folder it is?
-
Re: Unzip files and folders
- 02-05-2012, 12:34 AM #14
Re: Unzip files and folders
Create the FileOutputStream with the path where you want the file to be written.
- 02-05-2012, 02:45 AM #15
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Unzip files and folders
ok now I have this code:
But with some zip files it enters the while loop but with others "zipentry" has null value.Java Code:try { byte[] buffer = new byte[1024]; ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(ZipName)); ZipEntry zipentry = zipinputstream.getNextEntry(); System.out.println ("Start"); System.out.println (zipentry); while (zipentry != null) { String entryName = "" + zipentry.getName(); File newFile = new File(entryName); System.out.println ("Entered while loop 1"); if (zipentry.isDirectory()) { System.out.println ("Entered if 1"); if (!newFile.mkdirs()) { System.out.println ("Entered if 2"); break; } zipentry = zipinputstream.getNextEntry(); System.out.println ("Just Ending if 1"); continue; } System.out.println ("Ended if 1"); int n; FileOutputStream fileoutputstream = new FileOutputStream(entryName); while ((n = zipinputstream.read(buffer)) != -1) { //while ((n = zipinputstream.read(buffer, 0, 1024)) > -1) { System.out.println ("Entered while loop 2"); fileoutputstream.write(buffer, 0, n); } fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); System.out.println (zipentry); System.out.println ("Ended loop 1"); } zipinputstream.close(); System.out.println("EXTRACTED"); } catch (Exception e) { e.printStackTrace(); System.out.println("Coldn't extract game files"); }
- 02-05-2012, 03:07 AM #16
Re: Unzip files and folders
What is the difference between the two types of zip files?with some zip files it enters the while loop but with others "zipentry" has null value.
-
Re: Unzip files and folders
spam posts deleted, thread locked, and spammer blocked.
Similar Threads
-
Help please, Playing with folders and files.
By paulio2 in forum New To JavaReplies: 38Last Post: 11-17-2011, 02:16 PM -
Error in unzip for large files
By magesh joghee in forum Advanced JavaReplies: 1Last Post: 08-03-2011, 04:35 PM -
Access files in folders and sub-folders
By kakinyim in forum CLDC and MIDPReplies: 0Last Post: 05-18-2011, 09:29 PM -
Folders to group source files
By Skiller in forum New To JavaReplies: 6Last Post: 03-31-2011, 08:20 PM -
Searching directories for folders and .txt files
By XDrew574X in forum New To JavaReplies: 1Last Post: 03-29-2011, 09:41 PM


1Likes
LinkBack URL
About LinkBacks


Bookmarks