mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add YamlUtil
This commit is contained in:
@@ -451,11 +451,5 @@
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.tukaani</groupId>
|
||||
<artifactId>xz</artifactId>
|
||||
<version>1.9</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -6,7 +6,7 @@ import cn.hutool.extra.compress.archiver.Archiver;
|
||||
import cn.hutool.extra.compress.archiver.SevenZArchiver;
|
||||
import cn.hutool.extra.compress.archiver.StreamArchiver;
|
||||
import cn.hutool.extra.compress.extractor.Extractor;
|
||||
import cn.hutool.extra.compress.extractor.SenvenZExtractor;
|
||||
import cn.hutool.extra.compress.extractor.SevenZExtractor;
|
||||
import cn.hutool.extra.compress.extractor.StreamExtractor;
|
||||
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
||||
import org.apache.commons.compress.archivers.StreamingNotSupportedException;
|
||||
@@ -168,14 +168,14 @@ public class CompressUtil {
|
||||
*/
|
||||
public static Extractor createExtractor(Charset charset, String archiverName, File file) {
|
||||
if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(archiverName)) {
|
||||
return new SenvenZExtractor(file);
|
||||
return new SevenZExtractor(file);
|
||||
}
|
||||
try {
|
||||
return new StreamExtractor(charset, archiverName, file);
|
||||
} catch (CompressException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
if (cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")) {
|
||||
return new SenvenZExtractor(file);
|
||||
return new SevenZExtractor(file);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
@@ -218,14 +218,15 @@ public class CompressUtil {
|
||||
*/
|
||||
public static Extractor createExtractor(Charset charset, String archiverName, InputStream in) {
|
||||
if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(archiverName)) {
|
||||
return new SenvenZExtractor(in);
|
||||
return new SevenZExtractor(in);
|
||||
}
|
||||
|
||||
try {
|
||||
return new StreamExtractor(charset, archiverName, in);
|
||||
} catch (CompressException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
if (cause instanceof StreamingNotSupportedException && cause.getMessage().contains("7z")) {
|
||||
return new SenvenZExtractor(in);
|
||||
return new SevenZExtractor(in);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ public interface Extractor extends Closeable {
|
||||
* 释放(解压)到指定目录,结束后自动关闭流,此方法只能调用一次
|
||||
*
|
||||
* @param targetDir 目标目录
|
||||
* @param filter 解压文件过滤器,用于指定需要释放的文件,null表示不过滤。当{@link Filter#accept(Object)}为true时释放。
|
||||
* @param filter 解压文件过滤器,用于指定需要释放的文件,{@code null}表示不过滤。当{@link Filter#accept(Object)}为true时释放。
|
||||
*/
|
||||
void extract(File targetDir, Filter<ArchiveEntry> filter);
|
||||
|
||||
|
@@ -20,8 +20,9 @@ public class Seven7EntryInputStream extends InputStream {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param sevenZFile {@link SevenZFile}
|
||||
* @param entry {@link SevenZArchiveEntry}
|
||||
* @param entry {@link SevenZArchiveEntry}
|
||||
*/
|
||||
public Seven7EntryInputStream(SevenZFile sevenZFile, SevenZArchiveEntry entry) {
|
||||
this.sevenZFile = sevenZFile;
|
||||
@@ -30,13 +31,23 @@ public class Seven7EntryInputStream extends InputStream {
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
try{
|
||||
try {
|
||||
return Math.toIntExact(this.size);
|
||||
} catch (ArithmeticException e){
|
||||
} catch (ArithmeticException e) {
|
||||
throw new IOException("Entry size is too large!(max than Integer.MAX)", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取读取的长度(字节数)
|
||||
*
|
||||
* @return 读取的字节数
|
||||
* @since 5.7.14
|
||||
*/
|
||||
public long getReadSize() {
|
||||
return this.readSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
this.readSize++;
|
||||
|
@@ -5,6 +5,7 @@ import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
||||
@@ -14,6 +15,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.util.RandomAccess;
|
||||
|
||||
/**
|
||||
* 7z格式数据解压器,即将归档打包的数据释放
|
||||
@@ -21,7 +23,7 @@ import java.nio.channels.SeekableByteChannel;
|
||||
* @author looly
|
||||
* @since 5.5.0
|
||||
*/
|
||||
public class SenvenZExtractor implements Extractor {
|
||||
public class SevenZExtractor implements Extractor, RandomAccess {
|
||||
|
||||
private final SevenZFile sevenZFile;
|
||||
|
||||
@@ -30,7 +32,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
*
|
||||
* @param file 包文件
|
||||
*/
|
||||
public SenvenZExtractor(File file) {
|
||||
public SevenZExtractor(File file) {
|
||||
this(file, null);
|
||||
}
|
||||
|
||||
@@ -40,7 +42,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
* @param file 包文件
|
||||
* @param password 密码,null表示无密码
|
||||
*/
|
||||
public SenvenZExtractor(File file, char[] password) {
|
||||
public SevenZExtractor(File file, char[] password) {
|
||||
try {
|
||||
this.sevenZFile = new SevenZFile(file, password);
|
||||
} catch (IOException e) {
|
||||
@@ -53,7 +55,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
*
|
||||
* @param in 包流
|
||||
*/
|
||||
public SenvenZExtractor(InputStream in) {
|
||||
public SevenZExtractor(InputStream in) {
|
||||
this(in, null);
|
||||
}
|
||||
|
||||
@@ -63,7 +65,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
* @param in 包流
|
||||
* @param password 密码,null表示无密码
|
||||
*/
|
||||
public SenvenZExtractor(InputStream in, char[] password) {
|
||||
public SevenZExtractor(InputStream in, char[] password) {
|
||||
this(new SeekableInMemoryByteChannel(IoUtil.readBytes(in)), password);
|
||||
}
|
||||
|
||||
@@ -72,7 +74,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
*
|
||||
* @param channel {@link SeekableByteChannel}
|
||||
*/
|
||||
public SenvenZExtractor(SeekableByteChannel channel) {
|
||||
public SevenZExtractor(SeekableByteChannel channel) {
|
||||
this(channel, null);
|
||||
}
|
||||
|
||||
@@ -82,7 +84,7 @@ public class SenvenZExtractor implements Extractor {
|
||||
* @param channel {@link SeekableByteChannel}
|
||||
* @param password 密码,null表示无密码
|
||||
*/
|
||||
public SenvenZExtractor(SeekableByteChannel channel, char[] password) {
|
||||
public SevenZExtractor(SeekableByteChannel channel, char[] password) {
|
||||
try {
|
||||
this.sevenZFile = new SevenZFile(channel, password);
|
||||
} catch (IOException e) {
|
||||
@@ -107,6 +109,44 @@ public class SenvenZExtractor implements Extractor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取满足指定过滤要求的压缩包内的第一个文件流
|
||||
*
|
||||
* @param filter 用于指定需要释放的文件,null表示不过滤。当{@link Filter#accept(Object)}为true时返回对应流。
|
||||
* @return 满足过滤要求的第一个文件的流,无满足条件的文件返回{@code null}
|
||||
* @since 5.7.14
|
||||
*/
|
||||
public InputStream getFirst(Filter<ArchiveEntry> filter) {
|
||||
final SevenZFile sevenZFile = this.sevenZFile;
|
||||
for(SevenZArchiveEntry entry : sevenZFile.getEntries()){
|
||||
if(null != filter && false == filter.accept(entry)){
|
||||
continue;
|
||||
}
|
||||
if(entry.isDirectory()){
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
return sevenZFile.getInputStream(entry);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定名称的文件流
|
||||
*
|
||||
* @param entryName entry名称
|
||||
* @return 文件流,无文件返回{@code null}
|
||||
* @since 5.7.14
|
||||
*/
|
||||
public InputStream get(String entryName){
|
||||
return getFirst((entry)-> StrUtil.equals(entryName, entry.getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放(解压)到指定目录
|
||||
*
|
||||
@@ -120,6 +160,9 @@ public class SenvenZExtractor implements Extractor {
|
||||
SevenZArchiveEntry entry;
|
||||
File outItemFile;
|
||||
while (null != (entry = this.sevenZFile.getNextEntry())) {
|
||||
if(null != filter && false == filter.accept(entry)){
|
||||
continue;
|
||||
}
|
||||
outItemFile = FileUtil.file(targetDir, entry.getName());
|
||||
if (entry.isDirectory()) {
|
||||
// 创建对应目录
|
@@ -109,6 +109,9 @@ public class StreamExtractor implements Extractor{
|
||||
ArchiveEntry entry;
|
||||
File outItemFile;
|
||||
while (null != (entry = in.getNextEntry())) {
|
||||
if(null != filter && false == filter.accept(entry)){
|
||||
continue;
|
||||
}
|
||||
if (false == in.canReadEntryData(entry)) {
|
||||
// 无法读取的文件直接跳过
|
||||
continue;
|
||||
|
@@ -50,7 +50,7 @@ public class ArchiverTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void senvenZTest(){
|
||||
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)->{
|
||||
|
@@ -21,7 +21,7 @@ public class ExtractorTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void sevenZTest(){
|
||||
Extractor extractor = CompressUtil.createExtractor(
|
||||
Extractor extractor = CompressUtil.createExtractor(
|
||||
CharsetUtil.defaultCharset(),
|
||||
FileUtil.file("d:/test/compress/test.7z"));
|
||||
|
||||
|
Reference in New Issue
Block a user