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

ArchiveTool: improved log output

上级 d2c4c303
......@@ -54,6 +54,7 @@ public class ArchiveTool {
* @param args the command line arguments
*/
public static void main(String... args) throws Exception {
Log log = new Log();
if (args.length == 1) {
File f = new File(args[0]);
if (f.exists()) {
......@@ -82,16 +83,17 @@ public class ArchiveTool {
String toDir = args[2];
extract(fromFile, toDir);
} else {
System.out.println("An archive tool to efficiently compress large directories");
System.out.println("Command line options:");
System.out.println("<sourceDir>");
System.out.println("<compressedFile>");
System.out.println("-compress <compressedFile> <sourceDir>");
System.out.println("-extract <compressedFile> <targetDir>");
log.println("An archive tool to efficiently compress large directories");
log.println("Command line options:");
log.println("<sourceDir>");
log.println("<compressedFile>");
log.println("-compress <compressedFile> <sourceDir>");
log.println("-extract <compressedFile> <targetDir>");
}
}
private static void compress(String fromDir, String toFile) throws IOException {
final Log log = new Log();
final long start = System.currentTimeMillis();
final AtomicBoolean title = new AtomicBoolean();
long size = getSize(new File(fromDir), new Runnable() {
......@@ -104,18 +106,18 @@ public class ArchiveTool {
long now = System.currentTimeMillis();
if (now - lastTime > 3000) {
if (!title.getAndSet(true)) {
System.out.println("Counting files");
log.println("Counting files");
}
System.out.print(count + " ");
log.print(count + " ");
lastTime = now;
}
}
}
});
if (title.get()) {
System.out.println();
log.println();
}
System.out.println("Compressing " + size / MB + " MB");
log.println("Compressing " + size / MB + " MB");
InputStream in = getDirectoryInputStream(fromDir);
String temp = toFile + ".temp";
OutputStream out =
......@@ -125,21 +127,22 @@ public class ArchiveTool {
def.setLevel(Deflater.BEST_SPEED);
out = new BufferedOutputStream(
new DeflaterOutputStream(out, def), 1024 * 1024);
sort(in, out, temp, size);
sort(log, in, out, temp, size);
in.close();
out.close();
System.out.println();
System.out.println("Compressed to " +
log.println();
log.println("Compressed to " +
new File(toFile).length() / MB + " MB in " +
(System.currentTimeMillis() - start) / 1000 +
" seconds");
System.out.println();
log.println();
}
private static void extract(String fromFile, String toDir) throws IOException {
Log log = new Log();
long start = System.currentTimeMillis();
long size = new File(fromFile).length();
System.out.println("Extracting " + size / MB + " MB");
log.println("Extracting " + size / MB + " MB");
InputStream in =
new BufferedInputStream(
new FileInputStream(fromFile), 1024 * 1024);
......@@ -147,12 +150,12 @@ public class ArchiveTool {
Inflater inflater = new Inflater();
in = new InflaterInputStream(in, inflater, 1024 * 1024);
OutputStream out = getDirectoryOutputStream(toDir);
combine(in, out, temp);
combine(log, in, out, temp);
inflater.end();
in.close();
out.close();
System.out.println();
System.out.println("Extracted in " +
log.println();
log.println("Extracted in " +
(System.currentTimeMillis() - start) / 1000 +
" seconds");
}
......@@ -389,9 +392,8 @@ public class ArchiveTool {
};
}
private static void sort(InputStream in, OutputStream out,
private static void sort(Log log, InputStream in, OutputStream out,
String tempFileName, long size) throws IOException {
long lastTime = System.currentTimeMillis();
int bufferSize = 32 * 1024 * 1024;
DataOutputStream tempOut = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(tempFileName), 1024 * 1024));
......@@ -411,7 +413,7 @@ public class ArchiveTool {
break;
}
inPos += len;
lastTime = printProgress(lastTime, 0, 50, inPos, size);
log.printProgress(0, 50, inPos, size);
TreeMap<Chunk, Chunk> map = new TreeMap<Chunk, Chunk>();
for (int pos = 0; pos < len;) {
int[] key = getKey(bytes, pos, len);
......@@ -479,7 +481,7 @@ public class ArchiveTool {
last = c;
}
inPos += s.readNext();
lastTime = printProgress(lastTime, 50, 100, inPos, size);
log.printProgress(50, 100, inPos, size);
if (s.current != null) {
segmentIn.add(s);
}
......@@ -575,9 +577,8 @@ public class ArchiveTool {
return hash;
}
private static void combine(InputStream in, OutputStream out,
private static void combine(Log log, InputStream in, OutputStream out,
String tempFileName) throws IOException {
long lastTime = System.currentTimeMillis();
int bufferSize = 16 * 1024 * 1024;
DataOutputStream tempOut =
new DataOutputStream(
......@@ -614,7 +615,7 @@ public class ArchiveTool {
}
int length = c.value.length;
inPos += length;
lastTime = printProgress(lastTime, 0, 50, inPos, size);
log.printProgress(0, 50, inPos, size);
segmentSize += length;
for (long x : c.idList) {
map.put(x, c.value);
......@@ -657,7 +658,7 @@ public class ArchiveTool {
Chunk c = s.current;
dataOut.write(c.value);
inPos += s.readNext();
lastTime = printProgress(lastTime, 50, 100, inPos, size);
log.printProgress(50, 100, inPos, size);
if (s.current != null) {
segmentIn.add(s);
}
......@@ -812,6 +813,43 @@ public class ArchiveTool {
}
}
/**
* A logger, including context.
*/
static class Log {
private long lastTime;
private int pos;
void println() {
System.out.println();
}
void print(String msg) {
System.out.print(msg);
}
void println(String msg) {
System.out.println(msg);
}
void printProgress(int low, int high,
long current, long total) {
long now = System.currentTimeMillis();
if (now - lastTime > 3000) {
String msg = (low + (high - low) * current / total) + "% ";
if (pos > 80) {
System.out.println();
pos = 0;
}
System.out.print(msg);
pos += msg.length();
lastTime = now;
}
}
}
/**
* Write a variable size long value.
*
......@@ -861,14 +899,4 @@ public class ArchiveTool {
return x;
}
private static long printProgress(long lastTime, int low, int high,
long current, long total) {
long now = System.currentTimeMillis();
if (now - lastTime > 3000) {
System.out.print((low + (high - low) * current / total) + "% ");
lastTime = now;
}
return lastTime;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论