Here you can find the source of doZip(String filename, String zipFileName)
Parameter | Description |
---|---|
filename | a parameter |
zipFileName | a parameter |
public static void doZip(String filename, String zipFileName) throws Exception
//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); } } } }