提交 c7289919 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

ArchiveTool: configurable compression level

上级 8a6b058b
...@@ -18,6 +18,8 @@ import java.io.FileOutputStream; ...@@ -18,6 +18,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
...@@ -57,13 +59,14 @@ public class ArchiveTool { ...@@ -57,13 +59,14 @@ public class ArchiveTool {
*/ */
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
Log log = new Log(); Log log = new Log();
int level = Integer.getInteger("level", Deflater.BEST_SPEED);
if (args.length == 1) { if (args.length == 1) {
File f = new File(args[0]); File f = new File(args[0]);
if (f.exists()) { if (f.exists()) {
if (f.isDirectory()) { if (f.isDirectory()) {
String fromDir = f.getAbsolutePath(); String fromDir = f.getAbsolutePath();
String toFile = fromDir + ".at"; String toFile = fromDir + ".at";
compress(fromDir, toFile); compress(fromDir, toFile, level);
return; return;
} }
String fromFile = f.getAbsolutePath(); String fromFile = f.getAbsolutePath();
...@@ -79,7 +82,7 @@ public class ArchiveTool { ...@@ -79,7 +82,7 @@ public class ArchiveTool {
if ("-compress".equals(arg)) { if ("-compress".equals(arg)) {
String toFile = args[1]; String toFile = args[1];
String fromDir = args[2]; String fromDir = args[2];
compress(fromDir, toFile); compress(fromDir, toFile, level);
} else if ("-extract".equals(arg)) { } else if ("-extract".equals(arg)) {
String fromFile = args[1]; String fromFile = args[1];
String toDir = args[2]; String toDir = args[2];
...@@ -94,7 +97,7 @@ public class ArchiveTool { ...@@ -94,7 +97,7 @@ public class ArchiveTool {
} }
} }
private static void compress(String fromDir, String toFile) throws IOException { private static void compress(String fromDir, String toFile, int level) throws IOException {
final Log log = new Log(); final Log log = new Log();
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
final AtomicBoolean title = new AtomicBoolean(); final AtomicBoolean title = new AtomicBoolean();
...@@ -126,7 +129,7 @@ public class ArchiveTool { ...@@ -126,7 +129,7 @@ public class ArchiveTool {
new BufferedOutputStream( new BufferedOutputStream(
new FileOutputStream(toFile), 1024 * 1024); new FileOutputStream(toFile), 1024 * 1024);
Deflater def = new Deflater(); Deflater def = new Deflater();
def.setLevel(Deflater.BEST_SPEED); def.setLevel(level);
out = new BufferedOutputStream( out = new BufferedOutputStream(
new DeflaterOutputStream(out, def), 1024 * 1024); new DeflaterOutputStream(out, def), 1024 * 1024);
sort(log, in, out, temp, size); sort(log, in, out, temp, size);
...@@ -535,7 +538,10 @@ public class ArchiveTool { ...@@ -535,7 +538,10 @@ public class ArchiveTool {
long inPos = 0; long inPos = 0;
int bufferTotal = 64 * 1024 * 1024; int bufferTotal = 64 * 1024 * 1024;
int bufferPerStream = bufferTotal / segmentStart.size(); int bufferPerStream = bufferTotal / segmentStart.size();
// FileChannel fc = new RandomAccessFile(tempFileName, "r").getChannel();
for (int i = 0; i < segmentStart.size(); i++) { for (int i = 0; i < segmentStart.size(); i++) {
// long end = i < segmentStart.size() - 1 ? segmentStart.get(i+1) : fc.size();
// InputStream in = new SharedInputStream(fc, segmentStart.get(i), end);
InputStream in = new FileInputStream(tempFileName); InputStream in = new FileInputStream(tempFileName);
in.skip(segmentStart.get(i)); in.skip(segmentStart.get(i));
ChunkStream s = new ChunkStream(i); ChunkStream s = new ChunkStream(i);
...@@ -1056,4 +1062,37 @@ public class ArchiveTool { ...@@ -1056,4 +1062,37 @@ public class ArchiveTool {
return x; return x;
} }
static class SharedInputStream extends InputStream {
private final FileChannel channel;
private final long endPosition;
private long position;
SharedInputStream(FileChannel channel, long position, long endPosition) {
this.channel = channel;
this.position = position;
this.endPosition = endPosition;
}
@Override
public int read() {
throw new UnsupportedOperationException();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (len == 0) {
return 0;
}
len = (int) Math.min(len, endPosition - position);
if (len <= 0) {
return -1;
}
ByteBuffer buff = ByteBuffer.wrap(b, off, len);
len = channel.read(buff, position);
position += len;
return len;
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论