Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
i tried this but it can only unzip one file and i have to give the exact zip file name
but what i need do is i need to unzip all zip files in a folder only giving the folder path

Java
package unziptry;

/**
 *
 * @author Gayan
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class DeCompressZipFileExample {

    public static void main(String[] args) throws Exception {
        String zipFile = "C:\\Users\\Gayan\\Desktop\\gayan\\New_folder.zip";
        String outputFolder = "C:\\Users\\Gayan\\Desktop\\gayan";

        System.out.println("Begin unzip " + zipFile + " into " + outputFolder);
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String entryName = ze.getName();
            System.out.print("Extracting " + entryName + " -> " + outputFolder + File.separator + entryName + "...");
            File f = new File(outputFolder + File.separator + entryName);
            //create all folder needed to store in correct relative path.
            f.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(f);
            int len;
            byte buffer[] = new byte[1024];
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            System.out.println("OK!");
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();

        System.out.println(zipFile + " unzipped successfully");
    }
}
Posted
Comments
phil.o 6-Sep-13 8:21am    
Unclear : what does the code you provide actually does (I mean, what is the actual result, what are you waiting for, what is incorrect in the result you get)?
gayan_priyankara 6-Sep-13 8:38am    
this code only unzip the exact named zip file
what i want is , if i set the path only to the folder it should get all the .zip files and unzip all
Maarten Kools 6-Sep-13 9:04am    
So get all zip files in the directory and loop over them, it's not too hard to find in Google... http://www.tutorialspoint.com/java/io/file_listfiles_file_filter.htm
Richard MacCutchan 6-Sep-13 9:24am    
I think you need to call closeEntry() after processing each ZipEntry, not at the end of your loop.

1 solution

Hello Gayan,

Below a very simple code snippet shows how to retrieve zip files from the given path.
Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UnZipFiles {
    private static final Logger _LOGGER = LoggerFactory.getLogger(UnZipFiles.class);

    public void unzipAll(String path) {
        String filName;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles(); 
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                filName = listOfFiles[i].getName();
                if (files.endsWith(".zip") || files.endsWith(".ZIP")) {
                    unzipFile(listOfFiles[i]);
                }
            }
        }
    }

    public void unZipFile(File zipFile) {
        byte[] buffer = new byte[1024];
     
        try {
            // create output directory is not exists
            File folder = new File(OUTPUT_FOLDER);
            if (!folder.exists()) {
                folder.mkdir();
            }
     
            // get the zip file content
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            // get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
     
            while (ze != null) {
               String fileName = ze.getName();
               File newFile = new File(outputFolder + File.separator + fileName);
     
               if (_LOGGER.isDebugEnabled())
                   _LOGGER.debug("Unzipping file : {}", new Object[] {newFile.getAbsoluteFile()});
     
                // create all non exists folders
                // else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
     
                FileOutputStream fos = new FileOutputStream(newFile);             
     
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();   
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch(IOException ex) {
            _LOGGER.error("Exception occurred while unzipping!", ex);
        }
    }
}

Hope it helps.

Regards,
 
Share this answer
 
Comments
gayan_priyankara 8-Sep-13 2:52am    
Thank you
Prasad Khandekar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900