CompressUtil增加压缩和解压tgz(.tar.gz)文件

This commit is contained in:
Looly 2022-07-29 22:39:51 +08:00
parent 52dec0c301
commit 99d1835e38
5 changed files with 47 additions and 2 deletions

View File

@ -31,6 +31,7 @@
* 【extra 】 完善QrCodeUtil对于DATA_MATRIX生成的形状随机不可指定的功能pr#722@Gitee
* 【core 】 修改NetUtil.ipv6ToBigInteger原方法标记为过期pr#2485@Github
* 【core 】 ZipUtil新增zip文件解压大小限制防止zip炸弹pr#726@Gitee
* 【core 】 CompressUtil增加压缩和解压tgz.tar.gz文件issue#I5J33E@Gitee
*
### 🐞Bug修复
* 【core 】 修复CollUtil里面关于可变参数传null造成的crash问题pr#2428@Github

View File

@ -12,6 +12,7 @@ import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import java.io.File;
import java.io.IOException;
@ -78,6 +79,16 @@ public class StreamArchiver implements Archiver {
* @param targetStream 归档输出的流
*/
public StreamArchiver(Charset charset, String archiverName, OutputStream targetStream) {
if("tgz".equalsIgnoreCase(archiverName) || "tar.gz".equalsIgnoreCase(archiverName)){
//issue#I5J33E支持tgz格式解压
try {
this.out = new TarArchiveOutputStream(new GzipCompressorOutputStream(targetStream));
} catch (IOException e) {
throw new IORuntimeException(e);
}
return;
}
final ArchiveStreamFactory factory = new ArchiveStreamFactory(charset.name());
try {
this.out = factory.createArchiveOutputStream(archiverName, targetStream);
@ -129,7 +140,7 @@ public class StreamArchiver implements Archiver {
@Override
public void close() {
try {
try{
finish();
} catch (Exception ignore) {
//ignore

View File

@ -11,6 +11,8 @@ import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import java.io.File;
import java.io.IOException;
@ -72,6 +74,13 @@ public class StreamExtractor implements Extractor{
in = IoUtil.toBuffered(in);
if (StrUtil.isBlank(archiverName)) {
this.in = factory.createArchiveInputStream(in);
} else if("tgz".equalsIgnoreCase(archiverName) || "tar.gz".equalsIgnoreCase(archiverName)){
//issue#I5J33E支持tgz格式解压
try {
this.in = new TarArchiveInputStream(new GzipCompressorInputStream(in));
} catch (IOException e) {
throw new IORuntimeException(e);
}
} else {
this.in = factory.createArchiveInputStream(archiverName, in);
}

View File

@ -10,6 +10,7 @@ import org.junit.Test;
import java.io.File;
@SuppressWarnings("resource")
public class ArchiverTest {
@Test
@ -53,7 +54,19 @@ public class ArchiverTest {
public void sevenZTest(){
final File file = FileUtil.file("d:/test/compress/test.7z");
CompressUtil.createArchiver(CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.SEVEN_Z, file)
.add(FileUtil.file("d:/Java/apache-maven-3.6.3"), (f)->{
.add(FileUtil.file("d:/Java/apache-maven-3.8.1"), (f)->{
Console.log("Add: {}", f.getPath());
return true;
})
.finish().close();
}
@Test
@Ignore
public void tgzTest(){
final File file = FileUtil.file("d:/test/compress/test.tgz");
CompressUtil.createArchiver(CharsetUtil.CHARSET_UTF_8, "tgz", file)
.add(FileUtil.file("d:/Java/apache-maven-3.8.1"), (f)->{
Console.log("Add: {}", f.getPath());
return true;
})

View File

@ -27,4 +27,15 @@ public class ExtractorTest {
extractor.extract(FileUtil.file("d:/test/compress/test2/"));
}
@Test
@Ignore
public void tgzTest(){
Extractor extractor = CompressUtil.createExtractor(
CharsetUtil.defaultCharset(),
"tgz",
FileUtil.file("d:/test/test.tgz"));
extractor.extract(FileUtil.file("d:/test/tgz/"));
}
}