mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-18 06:27:01 +00:00
feat[zip]: unzip file
This commit is contained in:
@@ -12,12 +12,11 @@
|
||||
|
||||
package com.zfoo.net.util.security;
|
||||
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
import java.io.*;
|
||||
import java.util.zip.*;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -67,4 +66,30 @@ public abstract class ZipUtils {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public static void unzip(String zipFilePath, String destDirectory) {
|
||||
FileUtils.createDirectory(destDirectory);
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(zipFilePath);
|
||||
var zipIn = new ZipInputStream(fileInputStream);
|
||||
var entry = zipIn.getNextEntry();
|
||||
// 遍历ZIP文件中的所有条目
|
||||
while (entry != null) {
|
||||
var filePath = destDirectory + File.separator + entry.getName();
|
||||
if (!entry.isDirectory()) {
|
||||
// 如果条目是文件,则解压该文件
|
||||
FileUtils.writeInputStreamToFile(new File(filePath), zipIn);
|
||||
} else {
|
||||
// 如果条目是目录,则创建目录
|
||||
FileUtils.createDirectory(filePath);
|
||||
}
|
||||
zipIn.closeEntry();
|
||||
entry = zipIn.getNextEntry();
|
||||
}
|
||||
zipIn.close();
|
||||
} catch (Exception e) {
|
||||
IOUtils.closeIO(fileInputStream);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ package com.zfoo.net.util.security;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@@ -43,4 +45,8 @@ public class ZipTest {
|
||||
Assert.assertEquals(new String(bytes), str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipTest() throws IOException {
|
||||
ZipUtils.unzip("D:\\Project\\aaa\\ai-simulator.zip", "D:\\Project\\aaa\\bbb");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user