Java Zip Folder doZip(String filename, String zipFileName)

Here you can find the source of doZip(String filename, String zipFileName)

Description

This the compress xml file request and respone

License

Apache License

Parameter

Parameter Description
filename a parameter
zipFileName a parameter

Declaration

public static void doZip(String filename, String zipFileName) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.CRC32;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**// ww w  .  j  a  v  a  2s  .  c o m
     * This the compress xml file request and respone 
     * @param filename
     * @param zipFileName
     */
    public static void doZip(String filename, String zipFileName) throws Exception {

        File f = new File(filename);
        FileInputStream fis = null;
        ZipOutputStream zipOutputStream = null;
        try {
            if (f.exists()) {
                int size = (int) f.length();
                byte[] buf = new byte[size];
                fis = new FileInputStream(f.getAbsolutePath());
                fis.read(buf, 0, buf.length);
                CRC32 crc = new CRC32();
                zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
                zipOutputStream.setLevel(6);
                ZipEntry entry = new ZipEntry(f.getName());
                entry.setSize(buf.length);
                crc.reset();
                crc.update(buf);
                entry.setCrc(crc.getValue());
                zipOutputStream.putNextEntry(entry);
                zipOutputStream.write(buf, 0, buf.length);
                zipOutputStream.finish();
            } else {
                System.out.println(">> Input file :  " + f.getPath() + "/" + f.getName() + " Not exists .");
            }
        } catch (FileNotFoundException fNotFound) {
            throw new Exception(">> Error during compressing file ", fNotFound);
        } catch (IOException ioe) {
            throw new Exception(">> Error during compressing file ", ioe);
        } catch (Exception e) {
            throw new Exception(">> Error during compressing file ", e);
        } finally {
            try {
                if (zipOutputStream != null) {
                    zipOutputStream.close();
                }
                if (fis != null) {
                    fis.close();
                }

                boolean success = f.delete();
                if (!success) {
                    System.out.println("File : " + f.getName() + " Deletion failed.");
                } else {
                    System.out.println("File : " + f.getName() + " deleted. ");
                }

            } catch (IOException e) {
                throw new Exception(">> Error during closing Zip output Stream / deleting file . ", e);
            }
        }
    }
}

Related

  1. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
  2. doZip(File fileIn, String fileOut)
  3. doZip(Map zipStructure, File zipFile)
  4. doZip(Properties properties, String name, File f)
  5. doZip(String baseDir, String fileName)
  6. doZip(String inFilePath, String outFilePath)
  7. doZipFile(ZipOutputStream zipOut, File file, String dirPath)
  8. zip(File rootDir, String zipPath)
  9. zip(File rootDir, String zipPath)