简体   繁体   中英

Unzip Zip File Using JAVA ZipFile Class

I am trying to unzip files using JAVA and It is compiling without any errors. When i am calling it from my tool, and giving absolute destination path and source path of file eg: Source: D:\\data\\test.zip Destination: D:\\data\\op\\

I am getting Error like Acess is Denied (I have Admin access of system)

Stack trace:

Extracting: test/New Text Document - Copy (2).txt java.io.FileNotFoundException: D:\\Data\\Op (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.(FileOutputStream.java:179) at java.io.FileOutputStream.(FileOutputStream.java:70)

Below is the function I am calling, I believe it has to do with destination as it may ot extracting to absolute path but some temp folder where it cannot write. I tried some combination on destination but not working form my end.Please guide me how we can fix it.

public  void unzip(String zipFilePath, String destDir, String flName) throws Exception 
    {
     int BUFFER = 2048;//Buffer Size
 try 
     {
        File dir = new File(destDir);
        // Throw Exception if output directory doesn't exist

        if(!dir.exists()) 
        {
            //Print Message in Consol
        System.out.println("No Destination Directory Exists for Unzip Operation.");
        throw new Exception();
                }
         BufferedOutputStream dest = null;
         BufferedInputStream is = null;
         ZipEntry entry;
         ZipFile zipfile = new ZipFile(zipFilePath);
         Enumeration e = zipfile.entries();
         while(e.hasMoreElements()) 
         {
            entry = (ZipEntry) e.nextElement();
            System.out.println("Extracting: " +entry);
            is = new BufferedInputStream (zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[BUFFER];
            FileOutputStream fos = new FileOutputStream(destDir);
            dest = new BufferedOutputStream(fos, BUFFER);

            while ((count = is.read(data, 0, BUFFER)) != -1) 
              {
               dest.write(data, 0, count);
              }
              //Close All Streams
            dest.flush();
            dest.close();
            is.close();
          } 
         }
      catch(Exception e) 
          { 
         e.printStackTrace();
         throw new Exception();
          }

    }

The contents of a zip file can be both directories and files. While extracting, if the ZipEntry is a file, it can be extracted in a straightforward way by using BufferedOutputStream .write(). However, when ZipEntry is a directory, you can't do that because there is no directory present. Hence you have to create a new directory using the path.

Above code doesn't take directory into account, hence the files that have to be written into the new directory throws the exception mentioned in the question.

The below method fixes your problem.

public void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            byte[] bytesIn = new byte[BUFFER_SIZE];
            int read = 0;
            while ((read = zipIn.read(bytesIn)) != -1) {
                bos.write(bytesIn, 0, read);
            }
            bos.close();
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

You are trying to write to the directory

FileOutputStream fos = new FileOutputStream(destDir);

try changing it to

FileOutputStream fos = new FileOutputStream(destDir + File.separator + entry.getName ());

using the ZipFile entry 's name

A simple unzipping example from Oracle:

import java.io.*;
import java.util.zip.*;

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
          FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

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