// 在此输入java代码
public class ZipEncoding {
private String[] fileNames; //需压缩文件名集合
private String bundleUrl; //zip文件的所在文件包的路径
private String zipName; //zip的文件名
/**
*
* @param fileNames 文件名集合 以 ***.**格式
* @param zipName zip文件的文件名
* @param bundleUrl ./?????/
*/
public ZipEncoding(String[] fileNames,String zipName,
String bundleUrl){
this.fileNames = fileNames;
this.zipName = zipName + ".zip";
this.bundleUrl = bundleUrl;
}
public boolean zip() throws IOException{
boolean ok = false;
FileOutputStream f = new FileOutputStream(bundleUrl + zipName);
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out =
new ZipOutputStream(
new BufferedOutputStream(csum));
try{
//TODO delete
out.setComment(" A test of java zipping");
for(int i = 0 ; i < fileNames.length ; i ++){
if ( new File(bundleUrl + fileNames[i]).exists()){
BufferedReader in =
new BufferedReader(
new FileReader(bundleUrl + fileNames[i]));
out.putNextEntry(new ZipEntry(bundleUrl + fileNames[i]));
int c ;
while (( c = in.read()) != -1){
out.write(c);
}
in.close();
}else
continue;
}
ok = true;
}catch(Exception e){
throw new IOException(
"ZipEncoding.zip() error \n" + e.getMessage());
}finally{
out.close();
}
return ok;
}
public boolean unzip() throws IOException{
boolean ok = false;
return ok;
}
public static void main(String[] args){
try{
//String fileNames[] = {"513306020013.jpg","513306020014.jpg","513306020015.jpg"};
String fileNames[] = {"text1.txt","text2.txt","text3.txt"};
String zipName = "meinv";
String bundleUrl = "E:/";
ZipEncoding zip = new ZipEncoding(fileNames,zipName,bundleUrl);
boolean ok = zip.zip();
System.out.println("=======zip ok=======" + ok);
}catch(Exception e){
e.printStackTrace();
}
}
}
<p>
|