Closed HarelM closed 11 months ago
To zip a file (like in your first link):
const result = fflate.zipSync({ 'log.txt': fflate.strToU8('some text') });
// convert to a blob if you need
const resultBlob = new Blob([result]);
To unzip (like in your other link):
const zipData = new Uint8Array(await yourBlob.arrayBuffer());
const files = fflate.unzipSync(zipData, {
filter: file => file.name.startsWith("styles/") && file.name.endsWith(".json")
});
for (const fileName in files) {
const styleText = fflate.strFromU8(files[fileName]);
// do whatever you want here
}
If you care about not blocking the main thread, you can replace the zipSync
or unzipSync
calls with zip
or unzip
, e.g.
// first example
const result = await new Promise((resolve, reject) => fflate.zip(
{ 'log.txt': fflate.strToU8('some text') },
(err, result) => err ? reject(err) : resolve(result)
);
// second example
const files = await new Promise((resolve, reject) => fflate.unzip(zipData, {
filter: file => file.name.startsWith("styles/") && file.name.endsWith(".json")
}, (err, result) => err ? reject(err) : resolve(result)));
For reference, the ZIP documentation is quite thorough; you may have been looking in the wrong place.
What can't you do right now?
I can't understand how to create a zip file and how to read one with folders etc.
An optimal solution
Improve the docs.
(How) is this done by other libraries?
I'm using jszip where you can easily add a string and create a file, and browse directories.
I'm also using pako to gunzip, and I would like to use a single library for all my compression and decompression work. Currently the docs provided here for adding a zip file is partial - what is "chunk1" and "chunk2", how do I iterate over a folder etc...
Here are some of the examples I would like to migrate to this library: https://github.com/IsraelHikingMap/Site/blob/ba1c6a10ac40d6e429bbeb15778fef8479800a84/IsraelHiking.Web/src/application/services/file.service.ts#L156
https://github.com/IsraelHikingMap/Site/blob/ba1c6a10ac40d6e429bbeb15778fef8479800a84/IsraelHiking.Web/src/application/services/file.service.ts#L245
https://github.com/IsraelHikingMap/Site/blob/ba1c6a10ac40d6e429bbeb15778fef8479800a84/IsraelHiking.Web/src/application/services/file.service.ts#L257
Any help would be appreciated.