mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
65b9a047ea
commit
fe9e8bad84
@ -184,7 +184,7 @@ public class ZipReader implements Closeable {
|
||||
outItemFile.mkdirs();
|
||||
} else {
|
||||
// 文件
|
||||
FileUtil.writeFromStream(this.resource.get(zipEntry), outItemFile, false);
|
||||
FileUtil.copy(this.resource.get(zipEntry), outItemFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -938,18 +938,51 @@ public class FileUtil extends PathUtil {
|
||||
.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>
|
||||
* 如果目标文件为目录,则将源文件以相同文件名拷贝到目标目录
|
||||
*
|
||||
* @param srcPath 源文件或目录
|
||||
* @param destPath 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
|
||||
* @param targetPath 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
|
||||
* @param isOverride 是否覆盖目标文件
|
||||
* @return 目标目录或文件
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File copy(final String srcPath, final String destPath, final boolean isOverride) throws IORuntimeException {
|
||||
return copy(file(srcPath), file(destPath), isOverride);
|
||||
public static File copy(final String srcPath, final String targetPath, final boolean isOverride) throws IORuntimeException {
|
||||
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 {
|
||||
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
|
||||
|
||||
/**
|
||||
|
@ -279,16 +279,16 @@ public class PathUtil {
|
||||
* 通过JDK7+的 {@link Files#copy(InputStream, Path, CopyOption...)} 方法拷贝文件
|
||||
*
|
||||
* @param src 源文件流,使用后不闭流
|
||||
* @param target 目标文件或目录,如果为目录使用与源文件相同的文件名
|
||||
* @param out 目标流
|
||||
* @return 拷贝bytes数
|
||||
* @throws IORuntimeException IO异常
|
||||
* @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 !");
|
||||
|
||||
try {
|
||||
return Files.copy(src, target);
|
||||
return Files.copy(src, out);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
@ -41,6 +41,6 @@ public class PicTransferTest {
|
||||
private static void save(final ResultSet rs) throws SQLException{
|
||||
final String destDir = "d:/test/pic";
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -74,6 +74,10 @@
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
@ -106,6 +110,10 @@
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<groupId>commons-io</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
@ -496,7 +504,7 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId>
|
||||
<version>1.26.0</version>
|
||||
<version>1.26.1</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
@ -55,7 +55,8 @@ public class SevenZExtractor implements Extractor, RandomAccess {
|
||||
*/
|
||||
public SevenZExtractor(final File file, final char[] password) {
|
||||
try {
|
||||
this.sevenZFile = new SevenZFile(file, password);
|
||||
this.sevenZFile = SevenZFile.builder()
|
||||
.setFile(file).setPassword(password).get();
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
@ -97,7 +98,8 @@ public class SevenZExtractor implements Extractor, RandomAccess {
|
||||
*/
|
||||
public SevenZExtractor(final SeekableByteChannel channel, final char[] password) {
|
||||
try {
|
||||
this.sevenZFile = new SevenZFile(channel, password);
|
||||
this.sevenZFile = SevenZFile.builder()
|
||||
.setSeekableByteChannel(channel).setPassword(password).get();
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
@ -160,7 +162,7 @@ public class SevenZExtractor implements Extractor, RandomAccess {
|
||||
} else if (entry.hasStream()) {
|
||||
// 读取entry对应数据流
|
||||
// 此处直接读取而非调用sevenZFile.getInputStream(entry),因为此方法需要遍历查找entry对应位置,性能不好。
|
||||
FileUtil.writeFromStream(new Seven7EntryInputStream(sevenZFile, entry), outItemFile);
|
||||
FileUtil.copy(new Seven7EntryInputStream(sevenZFile, entry), outItemFile);
|
||||
} else {
|
||||
// 无数据流的文件创建为空文件
|
||||
FileUtil.touch(outItemFile);
|
||||
|
@ -39,7 +39,7 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
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) {
|
||||
if (in instanceof ArchiveInputStream) {
|
||||
this.in = (ArchiveInputStream) in;
|
||||
this.in = (ArchiveInputStream<?>) in;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ public class StreamExtractor implements Extractor {
|
||||
|
||||
@Override
|
||||
public InputStream getFirst(final Predicate<ArchiveEntry> predicate) {
|
||||
final ArchiveInputStream in = this.in;
|
||||
final ArchiveInputStream<?> in = this.in;
|
||||
ArchiveEntry entry;
|
||||
try {
|
||||
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 {
|
||||
Assert.isTrue(null != targetDir && ((!targetDir.exists()) || targetDir.isDirectory()), "target must be dir.");
|
||||
final ArchiveInputStream in = this.in;
|
||||
final ArchiveInputStream<?> in = this.in;
|
||||
ArchiveEntry entry;
|
||||
File outItemFile;
|
||||
while (null != (entry = in.getNextEntry())) {
|
||||
@ -173,7 +173,7 @@ public class StreamExtractor implements Extractor {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
outItemFile.mkdirs();
|
||||
} else {
|
||||
FileUtil.writeFromStream(in, outItemFile, false);
|
||||
FileUtil.copy(in, outItemFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user