Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f4321a

Browse files
authored
Merge pull request#862 from myfreeer/zip-options
zip: allow tuning compression for performance or size
2 parents62e1ec0 +bee0847 commit2f4321a

File tree

4 files changed

+109
-10
lines changed

4 files changed

+109
-10
lines changed

‎index.d.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,26 @@ export interface WorkbookProperties {
11271127
date1904:boolean;
11281128
}
11291129

1130+
exportinterfaceJSZipGeneratorOptions{
1131+
/**
1132+
*@default DEFLATE
1133+
*/
1134+
compression:'STORE'|'DEFLATE';
1135+
compressionOptions:null|{
1136+
/**
1137+
*@default 6
1138+
*/
1139+
level:number;
1140+
};
1141+
}
1142+
1143+
exportinterfaceXlsxWriteOptionsextendsstream.xlsx.WorkbookWriterOptions{
1144+
/**
1145+
* The option passed to JsZip#generateAsync(options)
1146+
*/
1147+
zip:Partial<JSZipGeneratorOptions>;
1148+
}
1149+
11301150
exportinterfaceXlsx{
11311151
/**
11321152
* read from a file
@@ -1153,17 +1173,17 @@ export interface Xlsx {
11531173
/**
11541174
* write to a buffer
11551175
*/
1156-
writeBuffer():Promise<Buffer>;
1176+
writeBuffer(options?:Partial<XlsxWriteOptions>):Promise<Buffer>;
11571177

11581178
/**
11591179
* write to a file
11601180
*/
1161-
writeFile(path:string):Promise<void>;
1181+
writeFile(path:string,options?:Partial<XlsxWriteOptions>):Promise<void>;
11621182

11631183
/**
11641184
* write to a stream
11651185
*/
1166-
write(stream:Stream):Promise<void>;
1186+
write(stream:Stream,options?:Partial<XlsxWriteOptions>):Promise<void>;
11671187
}
11681188

11691189
exportinterfaceCsvReadOptions{

‎lib/utils/zip-stream.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ class ZipReader extends events.EventEmitter {
102102
// The ZipWriter class
103103
// Packs streamed data into an output zip stream
104104
classZipWriterextendsevents.EventEmitter{
105-
constructor(){
105+
constructor(options){
106106
super();
107+
this.options=Object.assign({
108+
type:'nodebuffer',
109+
compression:'DEFLATE',
110+
},options);
107111

108112
this.zip=newJSZip();
109113
this.stream=newStreamBuf();
@@ -118,11 +122,7 @@ class ZipWriter extends events.EventEmitter {
118122
}
119123

120124
finalize(){
121-
constoptions={
122-
type:'nodebuffer',
123-
compression:'DEFLATE',
124-
};
125-
returnthis.zip.generateAsync(options).then(content=>{
125+
returnthis.zip.generateAsync(this.options).then(content=>{
126126
this.stream.end(content);
127127
this.emit('finish');
128128
});

‎lib/xlsx/xlsx.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class XLSX {
660660
write(stream,options){
661661
options=options||{};
662662
const{model}=this.workbook;
663-
constzip=newZipStream.ZipWriter();
663+
constzip=newZipStream.ZipWriter(options.zip);
664664
zip.pipe(stream);
665665

666666
this.prepareModel(model,options);

‎spec/integration/workbook/workbook.spec.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,86 @@ describe('Workbook', () => {
2929
testUtils.checkTestBook(wb2,'xlsx');
3030
});
3131
});
32+
describe('Xlsx Zip Compression',()=>{
33+
it('xlsx file with best compression',()=>{
34+
constwb=testUtils.createTestBook(newExcel.Workbook(),'xlsx');
3235

36+
returnwb.xlsx
37+
.writeFile(TEST_XLSX_FILE_NAME,{
38+
zip:{
39+
compression:'DEFLATE',
40+
compressionOptions:{
41+
level:9,
42+
},
43+
},
44+
})
45+
.then(()=>{
46+
constwb2=newExcel.Workbook();
47+
returnwb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
48+
})
49+
.then(wb2=>{
50+
testUtils.checkTestBook(wb2,'xlsx');
51+
});
52+
});
53+
54+
it('xlsx file with default compression',()=>{
55+
constwb=testUtils.createTestBook(newExcel.Workbook(),'xlsx');
56+
57+
returnwb.xlsx
58+
.writeFile(TEST_XLSX_FILE_NAME,{
59+
zip:{
60+
compression:'DEFLATE',
61+
},
62+
})
63+
.then(()=>{
64+
constwb2=newExcel.Workbook();
65+
returnwb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
66+
})
67+
.then(wb2=>{
68+
testUtils.checkTestBook(wb2,'xlsx');
69+
});
70+
});
71+
72+
73+
it('xlsx file with fast compression',()=>{
74+
constwb=testUtils.createTestBook(newExcel.Workbook(),'xlsx');
75+
76+
returnwb.xlsx
77+
.writeFile(TEST_XLSX_FILE_NAME,{
78+
zip:{
79+
compression:'DEFLATE',
80+
compressionOptions:{
81+
level:1,
82+
},
83+
},
84+
})
85+
.then(()=>{
86+
constwb2=newExcel.Workbook();
87+
returnwb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
88+
})
89+
.then(wb2=>{
90+
testUtils.checkTestBook(wb2,'xlsx');
91+
});
92+
});
93+
94+
it('xlsx file with no compression',()=>{
95+
constwb=testUtils.createTestBook(newExcel.Workbook(),'xlsx');
96+
97+
returnwb.xlsx
98+
.writeFile(TEST_XLSX_FILE_NAME,{
99+
zip:{
100+
compression:'STORE',
101+
},
102+
})
103+
.then(()=>{
104+
constwb2=newExcel.Workbook();
105+
returnwb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
106+
})
107+
.then(wb2=>{
108+
testUtils.checkTestBook(wb2,'xlsx');
109+
});
110+
});
111+
});
33112
it('sheets with correct names',()=>{
34113
constwb=newExcel.Workbook();
35114
constws1=wb.addWorksheet('Hello, World!');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp