Hi,
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
{
try
{
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);
file1.mkdir();
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);
file.mkdir();
//continue;
break;
}
BufferedReader bReader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
StringBuffer fileBuffer = new StringBuffer(" ");
String line ;
while ((line = bReader.readLine()) != null)
{
fileBuffer.append(line);
fileBuffer.append("\r\n");
//line = line + "\r\n";
//byte[] b = new byte[line.length()];
//b =line.getBytes();
//out.write(b);
}
String fileData = fileBuffer.toString();
File f1 = new File(path);
f1.createNewFile();
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);
}
out.write(data1);
out.close();
//bReader.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Thanks in Advance
Avinash