va] view plaincopyprint?
- package cn.cn;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.zip.Deflater;
-
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
-
- public class ZipUtil {
- private ZipFile zipFile;
- private ZipOutputStream zipOut;
- private int bufSize;
- private byte[] buf;
-
- public ZipUtil(){
-
- this.bufSize = 1024*4;
- this.buf = new byte[this.bufSize];
- }
-
-
- public void doZip(String srcFile, String destFile) {
- File zipFile = new File(srcFile);
- try {
-
- this.zipOut = new ZipOutputStream(new BufferedOutputStream(
- new FileOutputStream(destFile)));
-
- zipOut.setComment("comment");
-
- zipOut.setEncoding("GBK");
-
- zipOut.setMethod(ZipOutputStream.DEFLATED);
-
-
- zipOut.setLevel(Deflater.BEST_COMPRESSION);
-
- handleFile(zipFile, this.zipOut,"");
-
- this.zipOut.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
-
- private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException {
- System.out.println("遍历文件:"+zipFile.getName());
-
- if(zipFile.isDirectory())
- {
- File[] files = zipFile.listFiles();
-
- if (files.length == 0) {
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator));
- this.zipOut.closeEntry();
- } else {
- for (File file : files) {
-
- handleFile(file, zipOut, dirName+zipFile.getName()+File.separator);
- }
- }
- }
-
- else
- {
- FileInputStream fileIn = new FileInputStream(zipFile);
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()));
- int length = 0;
-
- while ((length = fileIn.read(this.buf)) > 0) {
- this.zipOut.write(this.buf, 0, length);
- }
-
- this.zipOut.closeEntry();
- }
-
- }
-
-
- public void unZip(String unZipfile, String destFile) {
- FileOutputStream fileOut;
- File file;
- InputStream inputStream;
-
- try {
-
- this.zipFile = new ZipFile(unZipfile);
-
- for (@SuppressWarnings("unchecked")
- Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries
- .hasMoreElements();) {
- ZipEntry entry = entries.nextElement();
-
- file = new File(destFile+File.separator+entry.getName());
-
- if (entry.isDirectory()) {
- file.mkdirs();
- } else {
-
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
- }
-
- inputStream = zipFile.getInputStream(entry);
-
- fileOut = new FileOutputStream(file);
- int length = 0;
-
- while ((length = inputStream.read(this.buf)) > 0) {
- fileOut.write(this.buf, 0, length);
- }
- fileOut.close();
- inputStream.close();
- }
- }
- this.zipFile.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }