mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-18 00:24:20 +00:00
feat[zip]: zip file
This commit is contained in:
@@ -66,6 +66,33 @@ public abstract class ZipUtils {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public static void zip(String[] sourceFilesPath, String zipFilePath) {
|
||||
FileOutputStream fos = null;
|
||||
ZipOutputStream zipOut = null;
|
||||
try {
|
||||
fos = new FileOutputStream(zipFilePath);
|
||||
zipOut = new ZipOutputStream(fos);
|
||||
for (var sourceFile : sourceFilesPath) {
|
||||
var fileToZip = new File(sourceFile);
|
||||
if (fileToZip.isHidden()) {
|
||||
continue;
|
||||
}
|
||||
var fis = new FileInputStream(fileToZip);
|
||||
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||
zipOut.putNextEntry(zipEntry);
|
||||
IOUtils.copy(fis, zipOut);
|
||||
IOUtils.closeIO(fis);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IOUtils.closeIO(zipOut, fos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void unzip(String zipFilePath, String destDirectory) {
|
||||
FileUtils.createDirectory(destDirectory);
|
||||
FileInputStream fileInputStream = null;
|
||||
|
||||
@@ -45,8 +45,16 @@ public class ZipTest {
|
||||
Assert.assertEquals(new String(bytes), str);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void unzipTest() throws IOException {
|
||||
public void zipTest() {
|
||||
var sourceFiles = new String[]{"D:\\Project\\aaa\\alibaba.mjs", "D:\\Project\\aaa\\baidu.mjs"
|
||||
, "D:\\Project\\aaa\\google.mjs", "D:\\Project\\aaa\\openai.mjs", "D:\\Project\\aaa\\tencent.mjs"};
|
||||
ZipUtils.zip(sourceFiles, "D:\\Project\\aaa\\ai-simulator.zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipTest() {
|
||||
ZipUtils.unzip("D:\\Project\\aaa\\ai-simulator.zip", "D:\\Project\\aaa\\bbb");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user