File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes unzipping files - works, but need hlp to make it better Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "unzipping files - works, but need hlp to make it better" Watch "unzipping files - works, but need hlp to make it better" New topic
Author

unzipping files - works, but need hlp to make it better

Madhu Chandra
Greenhorn

Joined: Jan 14, 2003
Posts: 2
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();

if (entry.isDirectory())
{
String dir = entry.getName();
System.out.println("entry :"+unzipPath + separator + courseFolderName + separator + dir);
checkFilePath(unzipPath + separator + courseFolderName + separator + dir);
unzippedFolders.addElement(courseFolderName + separator + dir);
}

}

//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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unzipping files - works, but need hlp to make it better
 
Similar Threads
Unzip
Unzip problem .. please its urgent
JAVA Unzipping XSLX
Fastest way to unzip a file
UnZip error