Problem in Unzipping the zip files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avik1612
    New Member
    • Sep 2007
    • 5

    #1

    Problem in Unzipping the zip files

    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.Buffere dReader;
    import java.io.File;
    import java.io.FileOut putStream;
    import java.io.Filenam eFilter;
    import java.io.IOExcep tion;
    import java.io.InputSt reamReader;
    import java.util.Enume ration;
    import java.util.zip.Z ipEntry;
    import java.util.zip.Z ipFile;


    // 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(e xt);
    }

    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.prin tln(" Extracting ...." + ss[j]) ;
    String zipFile = dir + ss[j];

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

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

    String folderName = zf.getName();
    folderName = folderName.subs tring(0, folderName.last IndexOf("."));
    File file1 = new File(folderName );
    file1.mkdir();

    while (entries.hasMor eElements())
    {
    ZipEntry ze = (ZipEntry) entries.nextEle ment();
    String path = directoryName + ze.getName() ;

    //System.out.prin tln(" : " + path);
    if (ze.getName().e ndsWith("/"))
    {
    File file = new File(path);
    file.mkdir();
    //continue;
    break;
    }

    BufferedReader bReader = new BufferedReader( new InputStreamRead er(zf.getInputS tream(ze)));
    StringBuffer fileBuffer = new StringBuffer(" ");
    String line ;

    while ((line = bReader.readLin e()) != null)
    {
    fileBuffer.appe nd(line);
    fileBuffer.appe nd("\r\n");
    //line = line + "\r\n";
    //byte[] b = new byte[line.length()];
    //b =line.getBytes( );
    //out.write(b);

    }

    String fileData = fileBuffer.toSt ring();
    File f1 = new File(path);
    f1.createNewFil e();
    FileOutputStrea m out = new FileOutputStrea m(path);
    //FileOutputStrea m out = new FileOutputStrea m(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.printStackTra ce();
    }
    }

    }


    Thanks in Advance

    Avinash
Working...