470,641 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Problem in Unzipping the zip files


I have created a program to unzip the file. It is unzipping the zip files when put in directory i.e folder inside folder but when i create a zip file without putting in a folder and try to unzip it. It unzips it but put the zipped file outside the folder created in the name of the zip file. I want it to be inside it.

Also when i do beyond compare of the two files i.e the zipped file and original file its shows difference.

What should i do

I have pasted the code below:

//Unzip the zip files to the folder with their name

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

// Getting the path to find the type of files
public class zipper implements FilenameFilter{
String ext;

public zipper(String ext)
this.ext="." + ext;

public boolean accept(File dir,String name)
return name.endsWith(ext);

public static void main(String args[]) throws IOException
String dir = "D:/";

File f2 = new File(dir);

FilenameFilter fn= new zipper("zip");
String ss[]=f2.list(fn);

for ( int j = 0; j < ss.length; j++)
System.out.println(" Extracting ...." + ss[j]) ;
String zipFile = dir + ss[j];

ZipFile zf = new ZipFile(zipFile);
Enumeration entries = zf.entries();

String directoryName = zf.getName();
directoryName = directoryName.substring(0, (directoryName.indexOf(":" + File.separator) + 2));

String folderName = zf.getName();
folderName = folderName.substring(0, folderName.lastIndexOf("."));
File file1 = new File(folderName);

while (entries.hasMoreElements())
ZipEntry ze = (ZipEntry) entries.nextElement();
String path = directoryName + ze.getName() ;

//System.out.println(" : " + path);
if (ze.getName().endsWith("/"))
File file = new File(path);

BufferedReader bReader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
StringBuffer fileBuffer = new StringBuffer(" ");
String line ;

while ((line = bReader.readLine()) != null)
//line = line + "\r\n";
//byte[] b = new byte[line.length()];
//b =line.getBytes();


String fileData = fileBuffer.toString();
File f1 = new File(path);
FileOutputStream out = new FileOutputStream(path);
//FileOutputStream out = new FileOutputStream(new File(folderName + "/" + ze.getName()));
long size = ze.getSize();

byte[] data1 = new byte[fileData.length()];

for (int i = 0; i < fileData.length(); i++)
data1[i] = (byte) fileData.charAt(i);
catch (Exception e)


Thanks in Advance

Sep 11 '07 #1
0 1857

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by thewhoracle | last post: by
1 post views Thread by batista | last post: by
reply views Thread by stjulian | last post: by
1 post views Thread by kss | last post: by
5 posts views Thread by Neil Crighton | last post: by
1 post views Thread by deepakd | last post: by
1 post views Thread by Korara | last post: by
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.