This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
unzipping files - works, but need hlp to make it better
Madhu Chandra
Greenhorn
Joined: Jan 14, 2003
Posts: 2
posted
0
Hi there! I am trying to unzip files using java.util.zip My code works If I have a parent directory for all files/folders. So when I unzip the structure is as - zipFileName -> parent folder -> child files/folders where, zipFileName and parent folder names are same. But I want to have it as parent folder -> child files/folders. Here is my code : [code] public void unzipFiles(String zipFileName, String zipFolderPath, String unzipPath) { Enumeration entries; ZipFile zipFile; try { //Set up a directory name for the unzipped files String courseFolderName = zipFileName.substring(0, zipFileName.indexOf(".")); //Get the zip file from its location in the "zip" folder on theServer String separator = File.separator; zipFile = new ZipFile(zipFolderPath + separator + zipFileName);
//Set up the directories in the data store entries = zipFile.entries();
while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement();
//Add the unzipped files to the data store entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); if (entry.isDirectory()) continue; if (debug) System.out.println("Extracting file: " + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(unzipPath + separator + courseFolderName + separator + entry.getName()))); } zipFile.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } [\code]
The above code works if the zip file is having all files under a parent folder: zipFileName -> parent folder -> child files/folders sample output: unzipping the files... entry :C:\DataStore\template\test2/IMAGES/ Creating directory C:\DataStore\template\test2/IMAGES/ entry :C:\DataStore\template\test2/INPUTS/ Creating directory C:\DataStore\template\test2/INPUTS/ otheriwse, if I have no levels under my zip level, I get an error like this: unzipping the files... Extracting file: schedule.htm Extracting file: IMAGES/AMTC.GIF java.io.FileNotFoundException: C:\DataStore\schedule\IMAGES/AMTC.GIF (The system cannot find the path specified) Can somebody pls help me find out where I am going wrong. Any help would be really really great. Thanks -Chandra [ February 12, 2003: Message edited by: Madhu Chandra ]