1
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package com.ruoyi.wisdomarbitrate.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class ZipFileUtils {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String filePath1 = "/path/to/file1.txt";
|
||||||
|
String filePath2 = "/path/to/file2.txt";
|
||||||
|
|
||||||
|
// ZIP文件的输出路径
|
||||||
|
String zipOutPath = "/path/to/output.zip";
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileOutputStream fous = new FileOutputStream(zipOutPath);
|
||||||
|
ZipOutputStream zipOutstream = new ZipOutputStream(fous);
|
||||||
|
FileInputStream fis1 = new FileInputStream(filePath1);
|
||||||
|
FileInputStream fis2 = new FileInputStream(filePath2);
|
||||||
|
|
||||||
|
zipFile(filePath1, fis1, zipOutstream);
|
||||||
|
|
||||||
|
zipFile(filePath2, fis2, zipOutstream);
|
||||||
|
|
||||||
|
System.out.println("文件成功打包成ZIP文件!");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static void zipFile(String zipfilePath, FileInputStream zipFileinsteam, ZipOutputStream zipfileOut)
|
||||||
|
throws IOException {
|
||||||
|
ZipEntry zipfileEntry = new ZipEntry(new File(zipfilePath).getName());
|
||||||
|
zipfileOut.putNextEntry(zipfileEntry);
|
||||||
|
|
||||||
|
// 从输入流读取数据并写入ZIP文件
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = zipFileinsteam.read(bytes)) >= 0) {
|
||||||
|
zipfileOut.write(bytes, 0, length);
|
||||||
|
}
|
||||||
|
zipfileOut.closeEntry();
|
||||||
|
zipFileinsteam.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user