How to compress an existing, uncompressed zip file #436
-
I'm using the AG Grid library in a project for work. The Excel export feature generates a (Opening the file in Excel and saving it will compress it, but I'd like the users to receive an already-compressed version.) Currently I'm working around the issue by reading the contents of the generated file and copying everything into a new zip file with zip.js. The new file is compressed by default so it ends up being much smaller than the original. What I want to know is if I can use zip.js to compress the initial file in place instead of copying its contents to a new file. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you want to implement this feature easily in 2 lines of code, you could use the import/export methods of the Here is below an example of code, it reads an uncompressed zip file and creates a compressed zip file from it. import * as zip from "https://unpkg.com/@zip.js/zip.js/index.js";
const uncompressedZipData = new Blob([new Uint8Array([
0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5C, 0xBA,
0x45, 0x40, 0xC8, 0x15, 0x94, 0x86, 0x8A, 0x04, 0x00, 0x00, 0x8A, 0x04,
0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x6C, 0x6F, 0x72, 0x65, 0x6D, 0x2E,
0x74, 0x78, 0x74, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73,
0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74,
0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65,
0x63, 0x74, 0x65, 0x74, 0x75, 0x65, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70,
0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2C,
0x20, 0x73, 0x65, 0x64, 0x20, 0x64, 0x69, 0x61, 0x6D, 0x20, 0x6E, 0x6F,
0x6E, 0x75, 0x6D, 0x6D, 0x79, 0x20, 0x6E, 0x69, 0x62, 0x68, 0x20, 0x65,
0x75, 0x69, 0x73, 0x6D, 0x6F, 0x64, 0x20, 0x74, 0x69, 0x6E, 0x63, 0x69,
0x64, 0x75, 0x6E, 0x74, 0x20, 0x75, 0x74, 0x20, 0x6C, 0x61, 0x6F, 0x72,
0x65, 0x65, 0x74, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x65, 0x20, 0x6D,
0x61, 0x67, 0x6E, 0x61, 0x20, 0x61, 0x6C, 0x69, 0x71, 0x75, 0x61, 0x6D,
0x20, 0x65, 0x72, 0x61, 0x74, 0x20, 0x76, 0x6F, 0x6C, 0x75, 0x74, 0x70,
0x61, 0x74, 0x2E, 0x20, 0x55, 0x74, 0x20, 0x77, 0x69, 0x73, 0x69, 0x20,
0x65, 0x6E, 0x69, 0x6D, 0x20, 0x61, 0x64, 0x20, 0x6D, 0x69, 0x6E, 0x69,
0x6D, 0x20, 0x76, 0x65, 0x6E, 0x69, 0x61, 0x6D, 0x2C, 0x20, 0x71, 0x75,
0x69, 0x73, 0x20, 0x6E, 0x6F, 0x73, 0x74, 0x72, 0x75, 0x64, 0x20, 0x65,
0x78, 0x65, 0x72, 0x63, 0x69, 0x20, 0x74, 0x61, 0x74, 0x69, 0x6F, 0x6E,
0x20, 0x75, 0x6C, 0x6C, 0x61, 0x6D, 0x63, 0x6F, 0x72, 0x70, 0x65, 0x72,
0x20, 0x73, 0x75, 0x73, 0x63, 0x69, 0x70, 0x69, 0x74, 0x20, 0x6C, 0x6F,
0x62, 0x6F, 0x72, 0x74, 0x69, 0x73, 0x20, 0x6E, 0x69, 0x73, 0x6C, 0x20,
0x75, 0x74, 0x20, 0x61, 0x6C, 0x69, 0x71, 0x75, 0x69, 0x70, 0x20, 0x65,
0x78, 0x20, 0x65, 0x61, 0x20, 0x63, 0x6F, 0x6D, 0x6D, 0x6F, 0x64, 0x6F,
0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x71, 0x75, 0x61, 0x74, 0x2E, 0x20,
0x44, 0x75, 0x69, 0x73, 0x20, 0x61, 0x75, 0x74, 0x65, 0x6D, 0x20, 0x76,
0x65, 0x6C, 0x20, 0x65, 0x75, 0x6D, 0x20, 0x69, 0x72, 0x69, 0x75, 0x72,
0x65, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x69, 0x6E, 0x20, 0x68,
0x65, 0x6E, 0x64, 0x72, 0x65, 0x72, 0x69, 0x74, 0x20, 0x69, 0x6E, 0x20,
0x76, 0x75, 0x6C, 0x70, 0x75, 0x74, 0x61, 0x74, 0x65, 0x20, 0x76, 0x65,
0x6C, 0x69, 0x74, 0x20, 0x65, 0x73, 0x73, 0x65, 0x20, 0x6D, 0x6F, 0x6C,
0x65, 0x73, 0x74, 0x69, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x71,
0x75, 0x61, 0x74, 0x2C, 0x20, 0x76, 0x65, 0x6C, 0x20, 0x69, 0x6C, 0x6C,
0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x65, 0x20, 0x65, 0x75,
0x20, 0x66, 0x65, 0x75, 0x67, 0x69, 0x61, 0x74, 0x20, 0x6E, 0x75, 0x6C,
0x6C, 0x61, 0x20, 0x66, 0x61, 0x63, 0x69, 0x6C, 0x69, 0x73, 0x69, 0x73,
0x20, 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x6F, 0x20, 0x65, 0x72, 0x6F,
0x73, 0x20, 0x65, 0x74, 0x20, 0x61, 0x63, 0x63, 0x75, 0x6D, 0x73, 0x61,
0x6E, 0x20, 0x65, 0x74, 0x20, 0x69, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x6F,
0x64, 0x69, 0x6F, 0x20, 0x64, 0x69, 0x67, 0x6E, 0x69, 0x73, 0x73, 0x69,
0x6D, 0x20, 0x71, 0x75, 0x69, 0x20, 0x62, 0x6C, 0x61, 0x6E, 0x64, 0x69,
0x74, 0x20, 0x70, 0x72, 0x61, 0x65, 0x73, 0x65, 0x6E, 0x74, 0x20, 0x6C,
0x75, 0x70, 0x74, 0x61, 0x74, 0x75, 0x6D, 0x20, 0x7A, 0x7A, 0x72, 0x69,
0x6C, 0x20, 0x64, 0x65, 0x6C, 0x65, 0x6E, 0x69, 0x74, 0x20, 0x61, 0x75,
0x67, 0x75, 0x65, 0x20, 0x64, 0x75, 0x69, 0x73, 0x20, 0x64, 0x6F, 0x6C,
0x6F, 0x72, 0x65, 0x20, 0x74, 0x65, 0x20, 0x66, 0x65, 0x75, 0x67, 0x61,
0x69, 0x74, 0x20, 0x6E, 0x75, 0x6C, 0x6C, 0x61, 0x20, 0x66, 0x61, 0x63,
0x69, 0x6C, 0x69, 0x73, 0x69, 0x2E, 0x20, 0x4E, 0x61, 0x6D, 0x20, 0x6C,
0x69, 0x62, 0x65, 0x72, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x6F, 0x72, 0x20,
0x63, 0x75, 0x6D, 0x20, 0x73, 0x6F, 0x6C, 0x75, 0x74, 0x61, 0x20, 0x6E,
0x6F, 0x62, 0x69, 0x73, 0x20, 0x65, 0x6C, 0x65, 0x69, 0x66, 0x65, 0x6E,
0x64, 0x20, 0x6F, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x63, 0x6F, 0x6E,
0x67, 0x75, 0x65, 0x20, 0x6E, 0x69, 0x68, 0x69, 0x6C, 0x20, 0x69, 0x6D,
0x70, 0x65, 0x72, 0x64, 0x69, 0x65, 0x74, 0x20, 0x64, 0x6F, 0x6D, 0x69,
0x6E, 0x67, 0x20, 0x69, 0x64, 0x20, 0x71, 0x75, 0x6F, 0x64, 0x20, 0x6D,
0x61, 0x7A, 0x69, 0x6D, 0x20, 0x70, 0x6C, 0x61, 0x63, 0x65, 0x72, 0x61,
0x74, 0x20, 0x66, 0x61, 0x63, 0x65, 0x72, 0x20, 0x70, 0x6F, 0x73, 0x73,
0x69, 0x6D, 0x20, 0x61, 0x73, 0x73, 0x75, 0x6D, 0x2E, 0x20, 0x54, 0x79,
0x70, 0x69, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x68, 0x61, 0x62, 0x65, 0x6E,
0x74, 0x20, 0x63, 0x6C, 0x61, 0x72, 0x69, 0x74, 0x61, 0x74, 0x65, 0x6D,
0x20, 0x69, 0x6E, 0x73, 0x69, 0x74, 0x61, 0x6D, 0x3B, 0x20, 0x65, 0x73,
0x74, 0x20, 0x75, 0x73, 0x75, 0x73, 0x20, 0x6C, 0x65, 0x67, 0x65, 0x6E,
0x74, 0x69, 0x73, 0x20, 0x69, 0x6E, 0x20, 0x69, 0x69, 0x73, 0x20, 0x71,
0x75, 0x69, 0x20, 0x66, 0x61, 0x63, 0x69, 0x74, 0x20, 0x65, 0x6F, 0x72,
0x75, 0x6D, 0x20, 0x63, 0x6C, 0x61, 0x72, 0x69, 0x74, 0x61, 0x74, 0x65,
0x6D, 0x2E, 0x20, 0x49, 0x6E, 0x76, 0x65, 0x73, 0x74, 0x69, 0x67, 0x61,
0x74, 0x69, 0x6F, 0x6E, 0x65, 0x73, 0x20, 0x64, 0x65, 0x6D, 0x6F, 0x6E,
0x73, 0x74, 0x72, 0x61, 0x76, 0x65, 0x72, 0x75, 0x6E, 0x74, 0x20, 0x6C,
0x65, 0x63, 0x74, 0x6F, 0x72, 0x65, 0x73, 0x20, 0x6C, 0x65, 0x67, 0x65,
0x72, 0x65, 0x20, 0x6D, 0x65, 0x20, 0x6C, 0x69, 0x75, 0x73, 0x20, 0x71,
0x75, 0x6F, 0x64, 0x20, 0x69, 0x69, 0x20, 0x6C, 0x65, 0x67, 0x75, 0x6E,
0x74, 0x20, 0x73, 0x61, 0x65, 0x70, 0x69, 0x75, 0x73, 0x2E, 0x20, 0x43,
0x6C, 0x61, 0x72, 0x69, 0x74, 0x61, 0x73, 0x20, 0x65, 0x73, 0x74, 0x20,
0x65, 0x74, 0x69, 0x61, 0x6D, 0x20, 0x70, 0x72, 0x6F, 0x63, 0x65, 0x73,
0x73, 0x75, 0x73, 0x20, 0x64, 0x79, 0x6E, 0x61, 0x6D, 0x69, 0x63, 0x75,
0x73, 0x2C, 0x20, 0x71, 0x75, 0x69, 0x20, 0x73, 0x65, 0x71, 0x75, 0x69,
0x74, 0x75, 0x72, 0x20, 0x6D, 0x75, 0x74, 0x61, 0x74, 0x69, 0x6F, 0x6E,
0x65, 0x6D, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x75, 0x65, 0x74, 0x75, 0x64,
0x69, 0x75, 0x6D, 0x20, 0x6C, 0x65, 0x63, 0x74, 0x6F, 0x72, 0x75, 0x6D,
0x2E, 0x20, 0x4D, 0x69, 0x72, 0x75, 0x6D, 0x20, 0x65, 0x73, 0x74, 0x20,
0x6E, 0x6F, 0x74, 0x61, 0x72, 0x65, 0x20, 0x71, 0x75, 0x61, 0x6D, 0x20,
0x6C, 0x69, 0x74, 0x74, 0x65, 0x72, 0x61, 0x20, 0x67, 0x6F, 0x74, 0x68,
0x69, 0x63, 0x61, 0x2C, 0x20, 0x71, 0x75, 0x61, 0x6D, 0x20, 0x6E, 0x75,
0x6E, 0x63, 0x20, 0x70, 0x75, 0x74, 0x61, 0x6D, 0x75, 0x73, 0x20, 0x70,
0x61, 0x72, 0x75, 0x6D, 0x20, 0x63, 0x6C, 0x61, 0x72, 0x61, 0x6D, 0x2C,
0x20, 0x61, 0x6E, 0x74, 0x65, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x69,
0x74, 0x20, 0x6C, 0x69, 0x74, 0x74, 0x65, 0x72, 0x61, 0x72, 0x75, 0x6D,
0x20, 0x66, 0x6F, 0x72, 0x6D, 0x61, 0x73, 0x20, 0x68, 0x75, 0x6D, 0x61,
0x6E, 0x69, 0x74, 0x61, 0x74, 0x69, 0x73, 0x20, 0x70, 0x65, 0x72, 0x20,
0x73, 0x65, 0x61, 0x63, 0x75, 0x6C, 0x61, 0x20, 0x71, 0x75, 0x61, 0x72,
0x74, 0x61, 0x20, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61, 0x20, 0x65, 0x74,
0x20, 0x71, 0x75, 0x69, 0x6E, 0x74, 0x61, 0x20, 0x64, 0x65, 0x63, 0x69,
0x6D, 0x61, 0x2E, 0x20, 0x45, 0x6F, 0x64, 0x65, 0x6D, 0x20, 0x6D, 0x6F,
0x64, 0x6F, 0x20, 0x74, 0x79, 0x70, 0x69, 0x2C, 0x20, 0x71, 0x75, 0x69,
0x20, 0x6E, 0x75, 0x6E, 0x63, 0x20, 0x6E, 0x6F, 0x62, 0x69, 0x73, 0x20,
0x76, 0x69, 0x64, 0x65, 0x6E, 0x74, 0x75, 0x72, 0x20, 0x70, 0x61, 0x72,
0x75, 0x6D, 0x20, 0x63, 0x6C, 0x61, 0x72, 0x69, 0x2C, 0x20, 0x66, 0x69,
0x61, 0x6E, 0x74, 0x20, 0x73, 0x6F, 0x6C, 0x6C, 0x65, 0x6D, 0x6E, 0x65,
0x73, 0x20, 0x69, 0x6E, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x75, 0x6D,
0x2E, 0x50, 0x4B, 0x01, 0x02, 0x14, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x00, 0x5C, 0xBA, 0x45, 0x40, 0xC8, 0x15, 0x94, 0x86, 0x8A, 0x04, 0x00,
0x00, 0x8A, 0x04, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C,
0x6F, 0x72, 0x65, 0x6D, 0x2E, 0x74, 0x78, 0x74, 0x50, 0x4B, 0x05, 0x06,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x37, 0x00, 0x00, 0x00,
0xB1, 0x04, 0x00, 0x00, 0x00, 0x00
])], { type: "application/zip" });
const zipFs = new zip.fs.FS();
await zipFs.importBlob(uncompressedZipData);
const compressedZipData = await zipFs.exportBlob();
console.log(compressedZipData); You can run the code here: https://plnkr.co/edit/jqye59hgw6i8eMlB However, from a technical point of view, this code does the same thing as yours, i.e. creating a new zip file. It does not update the original zip file, and this feature is not possible in zip.js. |
Beta Was this translation helpful? Give feedback.
If you want to implement this feature easily in 2 lines of code, you could use the import/export methods of the
fs.FS
API in zip.js.Here is below an example of code, it reads an uncompressed zip file and creates a compressed zip file from it.