| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.OutputStream; |
| 6 |
import java.io.PrintWriter; |
| 7 |
import java.util.zip.ZipOutputStream; |
| 8 |
|
| 9 |
import fileIO.ReadWrite; |
| 10 |
import fileIO.TextFile; |
| 11 |
import shared.Timer; |
| 12 |
|
| 13 |
public class TestCompressionSpeed { |
| 14 |
|
| 15 |
|
| 16 |
public static void main(String[] args){ |
| 17 |
|
| 18 |
TextFile tf=new TextFile(args[0], false); |
| 19 |
String[] lines=tf.toStringLines(); |
| 20 |
tf.close(); |
| 21 |
|
| 22 |
Timer t=new Timer(); |
| 23 |
|
| 24 |
for(int i=0; i<=9; i++){ |
| 25 |
t.start(); |
| 26 |
String fname=args[1].replaceFirst("#", ""+i); |
| 27 |
compress(lines, fname, i); |
| 28 |
t.stop(); |
| 29 |
|
| 30 |
System.out.println("Level "+i+" compress: "+t+" \tsize: "+new File(fname).length()); |
| 31 |
} |
| 32 |
|
| 33 |
for(int i=0; i<=9; i++){ |
| 34 |
t.start(); |
| 35 |
String fname=args[1].replaceFirst("#", ""+i); |
| 36 |
String[] lines2=read(fname); |
| 37 |
assert(lines2.length>=lines.length); |
| 38 |
t.stop(); |
| 39 |
|
| 40 |
System.out.println("Level "+i+" decompress: "+t); |
| 41 |
} |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public static void compress(String[] text, String fname, int level){ |
| 47 |
ReadWrite.ZIPLEVEL=level; |
| 48 |
OutputStream os=ReadWrite.getOutputStream(fname, false, true, true); |
| 49 |
PrintWriter writer=new PrintWriter(os); |
| 50 |
|
| 51 |
for(String s : text){writer.println(s);} |
| 52 |
for(String s : text){writer.println(s);} |
| 53 |
for(String s : text){writer.println(s);} |
| 54 |
for(String s : text){writer.println(s);} |
| 55 |
|
| 56 |
try { |
| 57 |
writer.flush(); |
| 58 |
if(os.getClass()==ZipOutputStream.class){ |
| 59 |
ZipOutputStream zos=(ZipOutputStream)os; |
| 60 |
zos.closeEntry(); |
| 61 |
zos.finish(); |
| 62 |
} |
| 63 |
writer.close(); |
| 64 |
} catch (IOException e) { |
| 65 |
|
| 66 |
e.printStackTrace(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public static String[] read(String fname){ |
| 72 |
TextFile tf=new TextFile(fname, false); |
| 73 |
String[] s=tf.toStringLines(); |
| 74 |
tf.close(); |
| 75 |
return s; |
| 76 |
} |
| 77 |
|
| 78 |
} |