简体   繁体   中英

zip Files are not unzipping in the same folder?

Here i have folder(ZipFilesFolder) in that it consist of 10 zip files say one.zip,two.zip,three.zip..ten.zip,i'm passing file every time from this folder to zipFileToUnzip as zipFilename.I need the result in the same folder(ZipFilesFolder)i need to unzip those files and instead of one.zip,two.zip,..one,two,three folder has to visible.

public static void zipFileToUnzip(File zipFilename) throws IOException {
    try {
        //String destinationname = "D:\\XYZ";
        byte[] buf = new byte[1024];
        ZipInputStream zipinputstream = null;
        ZipEntry zipentry;
        zipinputstream = new ZipInputStream(new FileInputStream(zipFilename));

        zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            //for each entry to be extracted
            String entryName = zipentry.getName();
            System.out.println("entryname " + entryName);
            int n;
            FileOutputStream fileoutputstream;
            File newFile = new File(entryName);
            String directory = newFile.getParent();

            if (directory == null) {
                if (newFile.isDirectory()) {
                    break;
                }
            }
            fileoutputstream = new FileOutputStream(
                    destinationname + entryName);
            while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }
            fileoutputstream.close();
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }//while
        zipinputstream.close();
    } catch (IOException e) {
    }
}

This is my code ,but it is not working,could anybody help me,how to get desired output.

There are a couple of problems with your code:

  • it does not compile since destinationname is commented, but referenced when opening the FileOutputStream
  • IOException s are caught and ignored. If you throw them you would get error messages that could help you diagnose the problem
  • when opening the FileOutputStream , you just concatenate two strings without adding a path-separator in between.
  • if the file to be created is in a directory, the directory is not created and thus FileOutputStream cannot create the file.
  • streams are not closed when exceptions occur.

If you do not mind using guava , which simplifies life when it comes to copying streams to files, you could use this code instead:

public static void unzipFile(File zipFile) throws IOException {
    File destDir = new File(zipFile.getParentFile(), Files.getNameWithoutExtension(zipFile.getName()));
    try(ZipInputStream zipStream = new ZipInputStream(new FileInputStream(zipFile))) {
        ZipEntry zipEntry = zipStream.getNextEntry();
        if(zipEntry == null) throw new IOException("Empty or no zip-file");
        while(zipEntry != null) {
            File destination = new File(destDir, zipEntry.getName());
            if(zipEntry.isDirectory()) {
                destination.mkdirs();
            } else {
                destination.getParentFile().mkdirs();
                Files.asByteSink(destination).writeFrom(zipStream);
            }
            zipEntry = zipStream.getNextEntry();
        }
    }
}

Alternatively you might also use zip4j , see also this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM