聞喘java.util.zip糞?猟周儿抹才盾儿
扮寂:2011-10-16
import java.io.*;
import java.util.zip.*;
/** *//**
*孔嬬:zip儿抹、盾儿
*傍苧:云殻會宥狛ZipOutputStream才ZipInputStream糞?阻zip儿抹才盾儿孔嬬.
*諒籾:喇噐java.util.zip淫旺音屶隔査忖,輝zip猟周嶄嗤兆忖葎嶄猟議猟周扮
, author by http://www.bt285.cn http://www.5a520.cn
* 祥氏竃?呟械:"Exception in thread "main " java.lang.IllegalArgumentExceptionBR> * at java.util.zip.ZipInputStream.getUTF8String (ZipInputStream.java:285)
*盾畳:
* 圭隈1、俐個import java.util.zip.ZipInputStream才ZipOutputStream.
* java.util.zip峪屶隔UTF-8,Ant戦中辛參峺協園鷹.
* 圭隈2、聞喘Apache Ant戦戻工議zip垢醤。
* 音聞喘java.util.zip議淫,委ant.jar慧欺classpath嶄.
* 殻會嶄聞喘import org.apache.tools.zip.*;
*
*叙工園殻僥楼歌深.
*
*@author Winty
*@date 2008-8-3
*@Usage:
* 儿抹:java Zip -zip "directoryName"
* 盾儿:java Zip -unzip "fileName.zip"
*/
public class Zip{
private ZipInputStream zipIn; //盾儿Zip
private ZipOutputStream zipOut; //儿抹Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes;
public Zip(){
this(512);
}
public Zip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
//儿抹猟周斜坪議猟周
public void doZip(String zipDirectory){//zipDirectoryPath:俶勣儿抹議猟周斜兆
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";//儿抹朔伏撹議zip猟周兆
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
//喇doZip距喘,弓拷頼撹朕村猟周響函
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){//泌惚朕村葎腎,夸汽鏡幹秀岻.
//ZipEntry議isDirectory()圭隈嶄,朕村參"/"潤硫.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{//泌惚朕村音葎腎,夸蛍艶侃尖朕村才猟周.
for(File fileName : files){
//System.out.println(fileName);
if(fileNam
|