Monday, February 4, 2013

How to work with zip files in Java

Java provides an API to work with compressed file format, zip. java.util.zip is the package that provides classes to compress and decompress files in zip format.

This is how to compress a file to zip file format. If we want to compress myfile.txt to myfilezip.zip into the output folder, first myfile.txt is retrieved via FileInputStream. Data read through FileInputStream writes to ZipOutputStream. ZipEntry provides a way to put file entries to ZipOutputStream by putNextEntry method.



public class ConvertFiletoZip {
       
    public void convertToZip(String file, String outputFolder) throws FileNotFoundException, IOException{
        try{
           
            //Gets myfile.txt as FileInputStream
            File myfile = new File(file);
            FileInputStream fileInputStream = new FileInputStream(myfile);            
           
            FileOutputStream outputStream = new FileOutputStream(outputFolder + "/myfilezip.zip");
            //Creates ZipOutputStream from FileOutputStream
            ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
           
            //Creates a ZipEntry and puts it to ZipOutputStream
            ZipEntry zipEntry = new ZipEntry("myfile");
            zipOutputStream.putNextEntry(zipEntry);        
                       
            byte[] buffer = new byte[(int) myfile.length()];
            int length = 0;
            //Reads bytes from FileInputStream and writes to ZipOutputStream
            while((length = fileInputStream.read(buffer))>0){
                zipOutputStream.write(buffer, 0, length);
            }
           
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            fileInputStream.close();
       
        }catch (IOException e) {
            System.out.println("Error occured when zip file is created");
        }             
    }
   
    public static void main(String[] args) throws FileNotFoundException, IOException{
        ConvertFiletoZip readZipFile = new ConvertFiletoZip();
        readZipFile.convertToZip("/home/kaushalya/myworkspace/ZipManipulation/myfile.txt","/home/kaushalya/myworkspace/ZipManipulation" );
       
    }
}



In the same way, we can read the zip file through  ZipInputStream that is  wrapped in FileInputStream.  getNextEntry() method is used to get the entries of the zip file. The data of the file entry read to a bytes array  is written through FileOutputStream to the output folder.


public class ConvertZiptoFile {

    public static final String ZIP_FILE = "/home/kaushalya/myworkspace/ZipManipulation/files/files.zip";
    public static final String OUTPUT_FOLDER = "/home/kaushalya/myworkspace/ZipManipulation/files";
   
    public void convertToZipToFile(String zipFile, String outputFolder){
        try {
            File folder = new File(OUTPUT_FOLDER);
            if(!folder.exists()){
                folder.mkdir();
            }
           
            FileInputStream fileInputStream = new FileInputStream(zipFile);
            ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
            ZipEntry zipEntry = null;
            while((zipEntry = zipInputStream.getNextEntry()) != null){
                System.out.println("File entry : " + zipEntry.getName());
                File unzippedFile = new File(folder+"/"+zipEntry.getName());
                int length = (int) unzippedFile.length();
                byte[] buffer = new byte[length];
               
                FileOutputStream fileOutputStream = new FileOutputStream(unzippedFile);
                int len =0;
                while((len =zipInputStream.read(buffer))>0){
                    fileOutputStream.write(buffer, 0, len);
                }
               
                zipEntry = null;
            }           
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
    }
   
    public static void main(String[] args){
        ConvertZiptoFile convertZiptoFile = new ConvertZiptoFile();
        convertZiptoFile.convertToZipToFile(ZIP_FILE, OUTPUT_FOLDER);
    }
}