Comments (1)
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.
From the README (click to expand)
// Note that the asynchronous version (see below) runs in parallel and
// is *much* (up to 3x) faster for larger archives.
const zipped = fflate.zipSync({
// Directories can be nested structures, as in an actual filesystem
'dir1': {
'nested': {
// You can use Unicode in filenames
'你好.txt': fflate.strToU8('Hey there!')
},
// You can also manually write out a directory path
'other/tmp.txt': new Uint8Array([97, 98, 99, 100])
},
// You can also provide compression options
'massiveImage.bmp': [aMassiveFile, {
level: 9,
mem: 12
}],
// PNG is pre-compressed; no need to waste time
'superTinyFile.png': [aPNGFile, { level: 0 }],
// Directories take options too
'exec': [{
'hello.sh': [fflate.strToU8('echo hello world'), {
// ZIP only: Set the operating system to Unix
os: 3,
// ZIP only: Make this file executable on Unix
attrs: 0o755 << 16
}]
}, {
// ZIP and GZIP support mtime (defaults to current time)
mtime: new Date('10/20/2020')
}]
}, {
// These options are the defaults for all files, but file-specific
// options take precedence.
level: 1,
// Obfuscate last modified time by default
mtime: new Date('1/1/1980')
});
// If you write the zipped data to myzip.zip and unzip, the folder
// structure will be outputted as:
// myzip.zip (original file)
// dir1
// |-> nested
// | |-> 你好.txt
// |-> other
// | |-> tmp.txt
// massiveImage.bmp
// superTinyFile.png
// When decompressing, folders are not nested; all filepaths are fully
// written out in the keys. For example, the return value may be:
// { 'nested/directory/structure.txt': Uint8Array(2) [97, 97] }
const decompressed = fflate.unzipSync(zipped, {
// You may optionally supply a filter for files. By default, all files in a
// ZIP archive are extracted, but a filter can save resources by telling
// the library not to decompress certain files
filter(file) {
// Don't decompress the massive image or any files larger than 10 MiB
return file.name != 'massiveImage.bmp' && file.originalSize <= 10_000_000;
}
});
from fflate.
Related Issues (20)
- demo or sample code for open local zip file HOT 1
- Streaming unzip bug HOT 5
- support `DecompressionStream` when available HOT 7
- [client side] [encrypted] 🔒when extract zip file - check if the zip file is encrypted or protected by a password HOT 2
- zlibSync compressed size is larger than pako HOT 4
- gunzipSync return array with 143.000.000 length HOT 2
- Dictionary support HOT 5
- Deflate stream produces way bigger outputs than Pako HOT 3
- Compression Streams ponyfill for browser HOT 1
- Deflate/Inflate does not work with zlib/gzip c++ HOT 7
- Add Bzip2?
- Hangs on gunzipSync of compressed content with invalid dictionary HOT 3
- Option to shorten output as much as possible HOT 3
- Incorrect argument order in AsyncFlateStreamHandler HOT 5
- Truncated output of gunzip if SIZE footer is incorrect HOT 5
- return uncompressed size instead of resized output buffer HOT 4
- The encoded data was not valid for encoding utf-8 HOT 1
- Streams onData do not work as expected anymore HOT 8
- CDN bundle for 0.8.1 is broken HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fflate.