add buffer

This commit is contained in:
Looly 2023-06-01 15:26:06 +08:00
parent f53cb2ef9d
commit ad2bae8a3d

View File

@ -61,6 +61,12 @@ public class ZipWriter implements Closeable {
private final ZipOutputStream out; private final ZipOutputStream out;
/**
* 自定义缓存大小
*/
private int bufferSize = IoUtil.DEFAULT_BUFFER_SIZE;
// region ----- Constructors
/** /**
* 构造 * 构造
* *
@ -68,7 +74,7 @@ public class ZipWriter implements Closeable {
* @param charset 编码 * @param charset 编码
*/ */
public ZipWriter(final File zipFile, final Charset charset) { public ZipWriter(final File zipFile, final Charset charset) {
this.out = getZipOutputStream(zipFile, charset); this(getZipOutputStream(zipFile, charset));
} }
/** /**
@ -78,7 +84,7 @@ public class ZipWriter implements Closeable {
* @param charset 编码 * @param charset 编码
*/ */
public ZipWriter(final OutputStream out, final Charset charset) { public ZipWriter(final OutputStream out, final Charset charset) {
this.out = ZipUtil.getZipOutputStream(out, charset); this(ZipUtil.getZipOutputStream(out, charset));
} }
/** /**
@ -89,6 +95,18 @@ public class ZipWriter implements Closeable {
public ZipWriter(final ZipOutputStream out) { public ZipWriter(final ZipOutputStream out) {
this.out = out; this.out = out;
} }
// endregion
/**
* 自定义压缩缓存大小特定条件下调节性能
*
* @param bufferSize 缓存大小
* @return this
*/
public ZipWriter setBufferSize(final int bufferSize) {
this.bufferSize = bufferSize;
return this;
}
/** /**
* 设置压缩级别可选1~9-1表示默认 * 设置压缩级别可选1~9-1表示默认
@ -293,10 +311,11 @@ public class ZipWriter implements Closeable {
*/ */
private ZipWriter putEntry(final String path, final InputStream in) throws IORuntimeException { private ZipWriter putEntry(final String path, final InputStream in) throws IORuntimeException {
final ZipEntry entry = new ZipEntry(path); final ZipEntry entry = new ZipEntry(path);
final ZipOutputStream out = this.out;
try { try {
out.putNextEntry(entry); out.putNextEntry(entry);
if (null != in) { if (null != in) {
IoUtil.copy(in, out); IoUtil.copy(in, out, bufferSize);
} }
out.closeEntry(); out.closeEntry();
} catch (final IOException e) { } catch (final IOException e) {
@ -305,7 +324,7 @@ public class ZipWriter implements Closeable {
IoUtil.closeQuietly(in); IoUtil.closeQuietly(in);
} }
IoUtil.flush(this.out); IoUtil.flush(out);
return this; return this;
} }
} }