2 module rezip
/*is aliced*/;
7 import iv
.vfs
.writers
.zip
;
10 string
n2s (ulong n
) {
14 if (left
== 0) { res
= ","~res
; left
= 3; }
15 res
= cast(char)('0'+n
%10)~res
;
17 } while ((n
/= 10) != 0);
22 void repackZip (string infname
, string outfname
) {
24 import std
.string
: format
;
25 import std
.exception
: collectException
;
26 import std
.file
: chdir
, getcwd
, mkdirRecurse
, remove
, rmdirRecurse
;
27 import std
.path
: expandTilde
;
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
, " ... ");
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!");
46 } catch (Exception e
) {
47 conwriteln("ERROR: ", e
.msg
);
55 void main (string
[] args
) {
56 ulong oldtotal
, newtotal
;
57 foreach (string ifname
; args
[1..$]) {
59 auto ofname
= ifname
~".$$$";
61 import std
.exception
: collectException
;
62 import std
.file
: 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
));
75 conwriteln("TOTAL: ", n2s(oldtotal
), " -> ", n2s(newtotal
), " saved: ", (oldtotal
< newtotal ?
"-" : ""), n2s(oldtotal
< newtotal ? newtotal
-oldtotal
: oldtotal
-newtotal
));