mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add null edit
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package cn.hutool.extra.compress;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.compress.archiver.Archiver;
|
||||
import cn.hutool.extra.compress.archiver.SevenZArchiver;
|
||||
import cn.hutool.extra.compress.archiver.StreamArchiver;
|
||||
@@ -8,6 +10,10 @@ import cn.hutool.extra.compress.extractor.SenvenZExtractor;
|
||||
import cn.hutool.extra.compress.extractor.StreamExtractor;
|
||||
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
||||
import org.apache.commons.compress.archivers.StreamingNotSupportedException;
|
||||
import org.apache.commons.compress.compressors.CompressorException;
|
||||
import org.apache.commons.compress.compressors.CompressorInputStream;
|
||||
import org.apache.commons.compress.compressors.CompressorOutputStream;
|
||||
import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
@@ -23,6 +29,62 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public class CompressUtil {
|
||||
|
||||
/**
|
||||
* 获取压缩输出流,用于压缩指定内容,支持的格式例如:
|
||||
* <ul>
|
||||
* <li>{@value CompressorStreamFactory#GZIP}</li>
|
||||
* <li>{@value CompressorStreamFactory#BZIP2}</li>
|
||||
* <li>{@value CompressorStreamFactory#XZ}</li>
|
||||
* <li>{@value CompressorStreamFactory#PACK200}</li>
|
||||
* <li>{@value CompressorStreamFactory#SNAPPY_FRAMED}</li>
|
||||
* <li>{@value CompressorStreamFactory#LZ4_BLOCK}</li>
|
||||
* <li>{@value CompressorStreamFactory#LZ4_FRAMED}</li>
|
||||
* <li>{@value CompressorStreamFactory#ZSTANDARD}</li>
|
||||
* <li>{@value CompressorStreamFactory#DEFLATE}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param compressorName 压缩名称,见:{@link CompressorStreamFactory}
|
||||
* @param out 输出流,可以输出到内存、网络或文件
|
||||
* @return {@link CompressorOutputStream}
|
||||
*/
|
||||
public CompressorOutputStream getOut(String compressorName, OutputStream out) {
|
||||
try {
|
||||
return new CompressorStreamFactory().createCompressorOutputStream(compressorName, out);
|
||||
} catch (CompressorException e) {
|
||||
throw new CompressException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取压缩输入流,用于解压缩指定内容,支持的格式例如:
|
||||
* <ul>
|
||||
* <li>{@value CompressorStreamFactory#GZIP}</li>
|
||||
* <li>{@value CompressorStreamFactory#BZIP2}</li>
|
||||
* <li>{@value CompressorStreamFactory#XZ}</li>
|
||||
* <li>{@value CompressorStreamFactory#PACK200}</li>
|
||||
* <li>{@value CompressorStreamFactory#SNAPPY_FRAMED}</li>
|
||||
* <li>{@value CompressorStreamFactory#LZ4_BLOCK}</li>
|
||||
* <li>{@value CompressorStreamFactory#LZ4_FRAMED}</li>
|
||||
* <li>{@value CompressorStreamFactory#ZSTANDARD}</li>
|
||||
* <li>{@value CompressorStreamFactory#DEFLATE}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param compressorName 压缩名称,见:{@link CompressorStreamFactory},null表示自动检测
|
||||
* @param in 输出流,可以输出到内存、网络或文件
|
||||
* @return {@link CompressorOutputStream}
|
||||
*/
|
||||
public CompressorInputStream getIn(String compressorName, InputStream in) {
|
||||
in = IoUtil.toMarkSupportStream(in);
|
||||
try {
|
||||
if(StrUtil.isBlank(compressorName)){
|
||||
compressorName = CompressorStreamFactory.detect(in);
|
||||
}
|
||||
return new CompressorStreamFactory().createCompressorInputStream(compressorName, in);
|
||||
} catch (CompressorException e) {
|
||||
throw new CompressException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建归档器,支持:
|
||||
* <ul>
|
||||
@@ -108,11 +170,11 @@ public class CompressUtil {
|
||||
if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(archiverName)) {
|
||||
return new SenvenZExtractor(file);
|
||||
}
|
||||
try{
|
||||
try {
|
||||
return new StreamExtractor(charset, archiverName, file);
|
||||
} catch (CompressException e){
|
||||
} catch (CompressException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
if(cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")){
|
||||
if (cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")) {
|
||||
return new SenvenZExtractor(file);
|
||||
}
|
||||
throw e;
|
||||
@@ -158,11 +220,11 @@ public class CompressUtil {
|
||||
if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(archiverName)) {
|
||||
return new SenvenZExtractor(in);
|
||||
}
|
||||
try{
|
||||
try {
|
||||
return new StreamExtractor(charset, archiverName, in);
|
||||
} catch (CompressException e){
|
||||
} catch (CompressException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
if(cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")){
|
||||
if (cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")) {
|
||||
return new SenvenZExtractor(in);
|
||||
}
|
||||
throw e;
|
||||
|
@@ -66,6 +66,15 @@ public class SevenZArchiver implements Archiver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取{@link SevenZOutputFile}以便自定义相关设置
|
||||
*
|
||||
* @return {@link SevenZOutputFile}
|
||||
*/
|
||||
public SevenZOutputFile getSevenZOutputFile() {
|
||||
return this.sevenZOutputFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SevenZArchiver add(File file, String path, Filter<File> filter) {
|
||||
try {
|
||||
@@ -93,9 +102,9 @@ public class SevenZArchiver implements Archiver {
|
||||
} catch (Exception ignore) {
|
||||
//ignore
|
||||
}
|
||||
if(null != out && this.channel instanceof SeekableInMemoryByteChannel){
|
||||
if (null != out && this.channel instanceof SeekableInMemoryByteChannel) {
|
||||
try {
|
||||
out.write(((SeekableInMemoryByteChannel)this.channel).array());
|
||||
out.write(((SeekableInMemoryByteChannel) this.channel).array());
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user