/
demo.ts
130 lines (110 loc) · 2.21 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Created by user on 2019/7/24.
*/
import JSZip from 'jszip';
import crypto from 'crypto';
import Bluebird from 'bluebird';
import { fixedJSZipDate } from '../index';
(async () => {
await Bluebird.props({
zip1: createZip()
.tap((data) => {
data.zip
// @ts-ignore
.folder("nested")
.file("hello.txt", "Hello World\n")
;
})
.then(generateZip),
zip2: createZip()
.tap((data) => {
data.zip
.file("nested/hello.txt", "Hello World\n")
;
})
.then(generateZip),
zip3: createZip()
.tap((data) => {
data.zip
.file("nested/hello.txt", "Hello World\n")
;
})
.then(generateZip),
zip4: createZip()
.tap((data) => {
data.zip
.folder("nested")
.file("hello.txt", "Hello World\n")
;
})
.then(generateZip),
})
.tap(ret => {
let bool = ret.zip1.md5 === ret.zip2.md5;
if (bool)
{
console.log(`zip1, zip2 md5 is same`)
}
else
{
console.error(`zip1, zip2 md5 is not same`)
}
let bool2 = ret.zip3.md5 === ret.zip2.md5;
console.log(`zip3 is same as zip2`, ':', bool2);
let bool3 = ret.zip1.md5 === ret.zip4.md5;
console.log(`zip1 is same as zip4`, ':', bool3);
console.dir(ret.zip1);
console.dir(ret.zip2);
console.dir({
zip1: ret.zip1.md5,
zip2: ret.zip2.md5,
zip3: ret.zip3.md5,
zip4: ret.zip4.md5,
})
})
;
})();
export function createZip()
{
return Bluebird.resolve()
.then(() => {
let zip = new JSZip();
let date = new Date('2019-07-24 06:00:00Z');
const options = {
date,
createFolders: false
};
zip.file("hello.txt", "Hello World\n", options);
return {
zip,
options,
}
})
}
export function generateZip(data: {
zip: JSZip,
options,
})
{
return Bluebird
.resolve(fixedJSZipDate(data.zip, data.options.date))
.then(() => {
return Bluebird.props({
...data,
md5: data.zip.generateAsync({
type: 'nodebuffer',
mimeType: 'application/epub+zip',
compression: 'DEFLATE',
compressionOptions: {
level: 9
},
})
.then(buf => {
const md5 = crypto.createHash('md5');
let result = md5.update(buf).digest('hex');
return result
})
})
})
;
}