/**
* 压缩工具
* @author xuchangcheng
* 2020年6月16日
*/
public class ZipUtils {
/**
* 多文件压缩为zip
* @param nFileList
* @param strZipName
*/
public static void multipleFiles2Zip(List<File> fileList,String zipName){
FileInputStream nFileInputStream = null;
ZipOutputStream nZipOutputStream = null;
try {
nZipOutputStream = new ZipOutputStream(new FileOutputStream(zipName));
for (File file : fileList) {
nFileInputStream = new FileInputStream(file);
nZipOutputStream.putNextEntry(new ZipEntry(file.getName()));
byte[] buf = new byte[2048];
int len;
while ((len = nFileInputStream.read(buf)) != -1) {
nZipOutputStream.write(len);
nZipOutputStream.flush();
}
}
nZipOutputStream.close();
nFileInputStream.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<File> fileList = new ArrayList<File>();
fileList.add(new File("E:/toutiao_video/448e56738fd54a4fb03cd5f1fd952ba9.mp4"));
String zipName = "E:/toutiao_video/112233.zip";
multipleFiles2Zip(fileList,zipName);
}
}