sq3: show SQLite error messages on stderr by default
[iv.d.git] / vfs / samples / rezip_simple.d
blob50566c3ec27f78946891d8de0615545ce6cd8d08
1 #!/usr/bin/env rdmd
2 module rezip /*is aliced*/;
4 import iv.alice;
5 import iv.cmdcon;
6 import iv.vfs;
7 import iv.vfs.writers.zip;
10 string n2s (ulong n) {
11 string res;
12 int left = 3;
13 do {
14 if (left == 0) { res = ","~res; left = 3; }
15 res = cast(char)('0'+n%10)~res;
16 --left;
17 } while ((n /= 10) != 0);
18 return res;
22 void repackZip (string infname, string outfname) {
23 import core.time;
24 import std.string : format;
25 import std.exception : collectException;
26 import std.file : chdir, getcwd, mkdirRecurse, remove, rmdirRecurse;
27 import std.path : expandTilde;
28 import std.process;
29 auto pakid = vfsAddPak(infname.expandTilde);
30 scope(exit) vfsRemovePak(pakid);
31 outfname = outfname.expandTilde;
32 collectException(outfname.remove());
33 auto fo = VFile(outfname, "w");
34 auto zw = new ZipWriter(fo);
35 scope(failure) if (zw.isOpen) zw.abort();
36 bool[string] fileseen;
37 foreach_reverse (const ref de; vfsFileList) {
38 if (de.name in fileseen) continue;
39 fileseen[de.name] = true;
40 conwrite(" ", de.name, " ... ");
41 try {
42 auto fl = VFile(de.name);
43 auto zidx = zw.pack(fl, de.name, zw.Method.Lzma);
44 if (zw.files[zidx].crc != de.crc32) throw new Exception("crc error!");
45 conwriteln("OK");
46 } catch (Exception e) {
47 conwriteln("ERROR: ", e.msg);
48 throw e;
51 zw.finish();
55 void main (string[] args) {
56 ulong oldtotal, newtotal;
57 foreach (string ifname; args[1..$]) {
58 import std.path;
59 auto ofname = ifname~".$$$";
60 scope(failure) {
61 import std.exception : collectException;
62 import std.file : remove;
63 ofname.remove();
65 conwriteln(":::[ ", ifname, " ]:::");
66 repackZip(ifname, ofname);
67 import std.file : rename, getSize;
68 auto oldsize = ifname.getSize;
69 auto newsize = ofname.getSize;
70 ofname.rename(ifname);
71 conwriteln(" ", n2s(oldsize), " -> ", n2s(newsize));
72 oldtotal += oldsize;
73 newtotal += newsize;
75 conwriteln("TOTAL: ", n2s(oldtotal), " -> ", n2s(newtotal), " saved: ", (oldtotal < newtotal ? "-" : ""), n2s(oldtotal < newtotal ? newtotal-oldtotal : oldtotal-newtotal));