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

ArchiveTool: improved log output

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