|
@@ -1,5 +1,8 @@
|
|
|
package api.common.util;
|
|
|
|
|
|
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
|
|
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
|
|
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -300,6 +303,57 @@ public class FileUtil {
|
|
|
copyBytes(in, response.getOutputStream(), bufferSize);
|
|
|
}
|
|
|
|
|
|
+ public static Set<String> decompress(InputStream inputStream, String targetDirectoryPath, String fileType) throws IOException {
|
|
|
+ Set<String> fileList = new HashSet<>();
|
|
|
+ if (!targetDirectoryPath.endsWith("/")) {
|
|
|
+ targetDirectoryPath += "/";
|
|
|
+ }
|
|
|
+ if (".tar".equals(fileType)) {
|
|
|
+ TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
|
|
|
+ TarArchiveEntry entry;
|
|
|
+ // 将 tar 文件解压到 extractPath 目录下
|
|
|
+ while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ createDirectory(targetDirectoryPath);
|
|
|
+ File currentFile = new File(targetDirectoryPath + entry.getName());
|
|
|
+ File parent = currentFile.getParentFile();
|
|
|
+ if (!parent.exists()) {
|
|
|
+ boolean mkdirs = parent.mkdirs();
|
|
|
+ }
|
|
|
+ fileList.add(currentFile.getAbsolutePath());
|
|
|
+ // 将文件写出到解压的目录
|
|
|
+ copyBytes(tarArchiveInputStream, new FileOutputStream(currentFile), 4096);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)) {
|
|
|
+ TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
|
|
|
+ TarArchiveEntry entry;
|
|
|
+ // 将 tar 文件解压到 extractPath 目录下
|
|
|
+ while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ createDirectory(targetDirectoryPath);
|
|
|
+ File currentFile = new File(targetDirectoryPath + entry.getName());
|
|
|
+ File parent = currentFile.getParentFile();
|
|
|
+ if (!parent.exists()) {
|
|
|
+ boolean mkdirs = parent.mkdirs();
|
|
|
+ }
|
|
|
+ fileList.add(currentFile.getAbsolutePath());
|
|
|
+ // 将文件写出到解压的目录
|
|
|
+ copyBytes(tarArchiveInputStream, new FileOutputStream(currentFile), 4096);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ return fileList;
|
|
|
+ }
|
|
|
+
|
|
|
// -------------------------------- F --------------------------------
|
|
|
// -------------------------------- G --------------------------------
|
|
|
|