02-zip文件打包

时间:2022-10-13 08:15:34
package com.day1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class Demo7 { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { copyFile(); } private static void copyFile() throws Exception { System.out.println("开始拷贝"); File src=new File("f:/src"); //源文件夹
File dest=new File("f:/dest"); //目标文件夹 File[] list = src.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
boolean b=true;
if(name.endsWith(".zip"))
{
b=false;
}
return b;
}
}); InputStream in=null;
OutputStream out=null; File zipFile=new File("f:/src/crm.zip");
FileOutputStream zipOut=new FileOutputStream(zipFile);
ZipOutputStream zip=new ZipOutputStream(zipOut);
zip.setComment("liuxunTEST") ; // 设置注释
for (File file : list) { File my=new File(dest,file.getName()); in=new FileInputStream(file);
out=new FileOutputStream(my);
int len=-1;
byte[] buffer=new byte[1024]; //while((len=in.read(buffer)) != -1)
//{
// out.write(buffer, 0, len);
//} //存入压缩包当中
ZipEntry entry=new ZipEntry(file.getName());
zip.putNextEntry(entry);
while((len=in.read(buffer)) != -1)
{
zip.write(buffer, 0, len);
} in.close();
out.close();
}
zip.close();
zipOut.close(); } }