This commit is contained in:
Looly 2024-03-12 00:25:48 +08:00
parent 65b9a047ea
commit fe9e8bad84
7 changed files with 60 additions and 81 deletions

View File

@ -184,7 +184,7 @@ public class ZipReader implements Closeable {
outItemFile.mkdirs(); outItemFile.mkdirs();
} else { } else {
// 文件 // 文件
FileUtil.writeFromStream(this.resource.get(zipEntry), outItemFile, false); FileUtil.copy(this.resource.get(zipEntry), outItemFile);
} }
} }
} }

View File

@ -938,18 +938,51 @@ public class FileUtil extends PathUtil {
.toFile(); .toFile();
} }
/**
* 通过JDK7+ Files#copy(InputStream, Path, CopyOption...) 方法拷贝文件
*
* @param src 源文件流使用后不关闭
* @param target 目标文件
* @param options {@link StandardCopyOption}
* @return 目标文件
* @throws IORuntimeException IO异常
* @since 5.8.27
*/
public static File copy(final InputStream src, final File target, final StandardCopyOption... options) throws IORuntimeException {
// check
Assert.notNull(src, "Source File is null !");
Assert.notNull(target, "Target File or directory is null !");
return copy(src, target.toPath(), options).toFile();
}
/**
* 将文件写入流中此方法不会关闭输出流
*
* @param src 文件
* @param out
* @return 写出的流byte数
* @throws IORuntimeException IO异常
* @since 6.0.0
*/
public static long copy(final File src, final OutputStream out) throws IORuntimeException {
// check
Assert.notNull(src, "Source File is null !");
Assert.notNull(out, "Target stream is null !");
return copy(src.toPath(), out);
}
/** /**
* 复制文件或目录<br> * 复制文件或目录<br>
* 如果目标文件为目录则将源文件以相同文件名拷贝到目标目录 * 如果目标文件为目录则将源文件以相同文件名拷贝到目标目录
* *
* @param srcPath 源文件或目录 * @param srcPath 源文件或目录
* @param destPath 目标文件或目录目标不存在会自动创建目录文件都创建 * @param targetPath 目标文件或目录目标不存在会自动创建目录文件都创建
* @param isOverride 是否覆盖目标文件 * @param isOverride 是否覆盖目标文件
* @return 目标目录或文件 * @return 目标目录或文件
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static File copy(final String srcPath, final String destPath, final boolean isOverride) throws IORuntimeException { public static File copy(final String srcPath, final String targetPath, final boolean isOverride) throws IORuntimeException {
return copy(file(srcPath), file(destPath), isOverride); return copy(file(srcPath), file(targetPath), isOverride);
} }
/** /**
@ -2464,70 +2497,6 @@ public class FileUtil extends PathUtil {
public static File writeBytes(final byte[] data, final File dest, final int off, final int len, final boolean isAppend) throws IORuntimeException { public static File writeBytes(final byte[] data, final File dest, final int off, final int len, final boolean isAppend) throws IORuntimeException {
return FileWriter.of(dest).write(data, off, len, isAppend); return FileWriter.of(dest).write(data, off, len, isAppend);
} }
/**
* 将流的内容写入文件<br>
* 此方法会自动关闭输入流
*
* @param target 目标文件
* @param in 输入流
* @return dest
* @throws IORuntimeException IO异常
*/
public static File writeFromStream(final InputStream in, final File target) throws IORuntimeException {
return writeFromStream(in, target, true);
}
/**
* 将流的内容写入文件
*
* @param target 目标文件
* @param in 输入流
* @param isCloseIn 是否关闭输入流
* @return dest
* @throws IORuntimeException IO异常
* @since 5.5.6
*/
public static File writeFromStream(final InputStream in, final File target, final boolean isCloseIn) throws IORuntimeException {
return FileWriter.of(target).writeFromStream(in, isCloseIn);
}
/**
* 将流的内容写入文件<br>
* 此方法会自动关闭输入流
*
* @param in 输入流
* @param fullFilePath 文件绝对路径
* @return 目标文件
* @throws IORuntimeException IO异常
*/
public static File writeFromStream(final InputStream in, final String fullFilePath) throws IORuntimeException {
return writeFromStream(in, touch(fullFilePath));
}
/**
* 将文件写入流中此方法不会关闭输出流
*
* @param file 文件
* @param out
* @return 写出的流byte数
* @throws IORuntimeException IO异常
*/
public static long writeToStream(final File file, final OutputStream out) throws IORuntimeException {
return FileReader.of(file).writeToStream(out);
}
/**
* 将路径对应文件写入流中此方法不会关闭输出流
*
* @param fullFilePath 文件绝对路径
* @param out 输出流
* @return 写出的流byte数
* @throws IORuntimeException IO异常
*/
public static long writeToStream(final String fullFilePath, final OutputStream out) throws IORuntimeException {
return writeToStream(touch(fullFilePath), out);
}
// endregion // endregion
/** /**

View File

@ -279,16 +279,16 @@ public class PathUtil {
* 通过JDK7+ {@link Files#copy(InputStream, Path, CopyOption...)} 方法拷贝文件 * 通过JDK7+ {@link Files#copy(InputStream, Path, CopyOption...)} 方法拷贝文件
* *
* @param src 源文件流使用后不闭流 * @param src 源文件流使用后不闭流
* @param target 目标文件或目录如果为目录使用与源文件相同的文件名 * @param out 目标流
* @return 拷贝bytes数 * @return 拷贝bytes数
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 6.0.0 * @since 6.0.0
*/ */
public static long copy(final Path src, final OutputStream target) throws IORuntimeException { public static long copy(final Path src, final OutputStream out) throws IORuntimeException {
Assert.notNull(src, "Source is null !"); Assert.notNull(src, "Source is null !");
try { try {
return Files.copy(src, target); return Files.copy(src, out);
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }

View File

@ -41,6 +41,6 @@ public class PicTransferTest {
private static void save(final ResultSet rs) throws SQLException{ private static void save(final ResultSet rs) throws SQLException{
final String destDir = "d:/test/pic"; final String destDir = "d:/test/pic";
final String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP")); final String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP"));
FileUtil.writeFromStream(rs.getBlob("PIC").getBinaryStream(), path); FileUtil.copy(rs.getBlob("PIC").getBinaryStream(), FileUtil.file(path));
} }
} }

View File

@ -74,6 +74,10 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
</exclusion> </exclusion>
<exclusion>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
</exclusions> </exclusions>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
@ -106,6 +110,10 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
</exclusion> </exclusion>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions> </exclusions>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
@ -496,7 +504,7 @@
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId> <artifactId>commons-compress</artifactId>
<version>1.26.0</version> <version>1.26.1</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

View File

@ -55,7 +55,8 @@ public class SevenZExtractor implements Extractor, RandomAccess {
*/ */
public SevenZExtractor(final File file, final char[] password) { public SevenZExtractor(final File file, final char[] password) {
try { try {
this.sevenZFile = new SevenZFile(file, password); this.sevenZFile = SevenZFile.builder()
.setFile(file).setPassword(password).get();
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
@ -97,7 +98,8 @@ public class SevenZExtractor implements Extractor, RandomAccess {
*/ */
public SevenZExtractor(final SeekableByteChannel channel, final char[] password) { public SevenZExtractor(final SeekableByteChannel channel, final char[] password) {
try { try {
this.sevenZFile = new SevenZFile(channel, password); this.sevenZFile = SevenZFile.builder()
.setSeekableByteChannel(channel).setPassword(password).get();
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
@ -160,7 +162,7 @@ public class SevenZExtractor implements Extractor, RandomAccess {
} else if (entry.hasStream()) { } else if (entry.hasStream()) {
// 读取entry对应数据流 // 读取entry对应数据流
// 此处直接读取而非调用sevenZFile.getInputStream(entry)因为此方法需要遍历查找entry对应位置性能不好 // 此处直接读取而非调用sevenZFile.getInputStream(entry)因为此方法需要遍历查找entry对应位置性能不好
FileUtil.writeFromStream(new Seven7EntryInputStream(sevenZFile, entry), outItemFile); FileUtil.copy(new Seven7EntryInputStream(sevenZFile, entry), outItemFile);
} else { } else {
// 无数据流的文件创建为空文件 // 无数据流的文件创建为空文件
FileUtil.touch(outItemFile); FileUtil.touch(outItemFile);

View File

@ -39,7 +39,7 @@ import java.util.function.Predicate;
*/ */
public class StreamExtractor implements Extractor { public class StreamExtractor implements Extractor {
private final ArchiveInputStream in; private final ArchiveInputStream<?> in;
/** /**
* 构造 * 构造
@ -81,7 +81,7 @@ public class StreamExtractor implements Extractor {
*/ */
public StreamExtractor(final Charset charset, final String archiverName, InputStream in) { public StreamExtractor(final Charset charset, final String archiverName, InputStream in) {
if (in instanceof ArchiveInputStream) { if (in instanceof ArchiveInputStream) {
this.in = (ArchiveInputStream) in; this.in = (ArchiveInputStream<?>) in;
return; return;
} }
@ -109,7 +109,7 @@ public class StreamExtractor implements Extractor {
@Override @Override
public InputStream getFirst(final Predicate<ArchiveEntry> predicate) { public InputStream getFirst(final Predicate<ArchiveEntry> predicate) {
final ArchiveInputStream in = this.in; final ArchiveInputStream<?> in = this.in;
ArchiveEntry entry; ArchiveEntry entry;
try { try {
while (null != (entry = in.getNextEntry())) { while (null != (entry = in.getNextEntry())) {
@ -156,7 +156,7 @@ public class StreamExtractor implements Extractor {
*/ */
private void extractInternal(final File targetDir, final Predicate<ArchiveEntry> predicate) throws IOException { private void extractInternal(final File targetDir, final Predicate<ArchiveEntry> predicate) throws IOException {
Assert.isTrue(null != targetDir && ((!targetDir.exists()) || targetDir.isDirectory()), "target must be dir."); Assert.isTrue(null != targetDir && ((!targetDir.exists()) || targetDir.isDirectory()), "target must be dir.");
final ArchiveInputStream in = this.in; final ArchiveInputStream<?> in = this.in;
ArchiveEntry entry; ArchiveEntry entry;
File outItemFile; File outItemFile;
while (null != (entry = in.getNextEntry())) { while (null != (entry = in.getNextEntry())) {
@ -173,7 +173,7 @@ public class StreamExtractor implements Extractor {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
outItemFile.mkdirs(); outItemFile.mkdirs();
} else { } else {
FileUtil.writeFromStream(in, outItemFile, false); FileUtil.copy(in, outItemFile);
} }
} }
} }