public static String zipFiles(ArrayList<String> listFile, String zipName){
//System.out.println("listFile__________________" + listFile.size());
byte[] buf = new byte[1024];
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);// 获取年
int yue = now.get(Calendar.MONTH) + 1;// 获取月
int ri = now.get(Calendar.DAY_OF_MONTH);// 获取日
long t1 = System.currentTimeMillis();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentTime = "_" + df.format(t1).toString();
String zipFileName = "";
if(!zipName.equals("")){
zipFileName = getHome() + "/model/UI/LandManagerSys/doc/" + zipName + currentTime + ".zip";
}
File zipFile = new File(zipFileName);
try {
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));//完成文件或文件夹的压缩
for(int m=0; m<listFile.size(); m++){
File fFIle = new File(listFile.get(m));
FileInputStream inputStream = new FileInputStream(fFIle);
zipOut.putNextEntry(new ZipEntry(fFIle.getName()));
int len;
while((len = inputStream.read(buf)) > 0){
zipOut.write(buf, 0, len);
}
zipOut.closeEntry();
inputStream.close();
}
zipOut.close();
System.out.println("压缩完成!");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return zipFile.getName();
}
压缩文件.zip
最新推荐文章于 2022-10-16 23:43:11 发布