The script zip-fs.js
helps to manipulate zip files easily. It provides a virtual filesystem API
implementation residing in RAM for zip files.
npm install @zip.js/zip.js
Then, you can import it as a JavaScript module in your project and bundle it with your favorite
tools:
import
import { fs, configure } from "@zip.js/zip.js";
require
const { fs, configure } = require("@zip.js/zip.js");
zip-fs.min.js
from the
/dist
directory in your project. If you don't want to use web workers, add
zip-fs-full.min.js
instead. Then, include zip.min.js
(or
zip-fs-full.min.js
) in your HTML page:
<script type="text/javascript" src="/library_path/zip-fs.min.js"></script>
<script>
const { fs, configure } = window.zip;
// ...
</script>
require(["/library_path/zip-fs.min.js"], zip => {
const { fs, configure } = zip;
// ...
});
useWebWorkers
set to
false
zip.configure({
useWebWorkers: false
});
Zip filesystem constructor
zip.fs.FS()
A zip.fs.FS
object stores an entire filesystem. The
root
property stores a ZipDirectoryEntry
object
representing the root directory of the zip. The FS
class inherits
from all the methods of the class ZipDirectoryEntry
in order
to modify data in the root
directory without accessing to it explicitely.
root (ZipDirectoryEntry
)
ZipDirectoryEntry
object referencing the root
directory of the filesystemFS#getById(entryId)
Get a ZipEntry
object given a unique entry id
Parameters:
entryId (number)
Returns:
ZipEntry
ZipEntry
objectFS#find(fullname)
Get a ZipEntry
object given a unique entry full filename
Parameters:
fullname (string)
Returns:
ZipEntry
ZipEntry
objectFS#remove(zipEntry)
Remove a ZipEntry
object from the virtual filesystem
Parameters:
zipEntry (ZipEntry)
FS#move(entry, destination)
Move a ZipEntry
object into the
ZipDirectoryEntry
destination object
Parameters:
entry (ZipEntry)
ZipEntry
to movedestination (ZipDirectoryEntry)
ZipDirectoryEntry
destination objectZipEntry constructor
ZipEntry()
Each ZipEntry
object represents a node in the zip directory tree. A
file in the zip is a ZipFileEntry
with a
ZipEntry
object as prototype. A directory in the zip is a
ZipDirectoryEntry
with a
ZipEntry
object as prototype.
name (string)
directory (boolean)
true
if the entry is a ZipDirectoryEntry
objectchildren (Array of ZipEntry)
ZipFileEntry
objectid (number)
ZipEntry#getFullname()
Get the full name of the entry
Returns:
string
ZipEntry#getRelativeName(ancestor)
Get the relative name of the entry to the ancestor
Parameters:
ancestor (ZipDirectoryEntry)
Returns:
string
ZipEntry#isDescendantOf(ancestor)
Test if the entry is a descendant of ancestor
directory entry
Parameters:
ancestor (ZipDirectoryEntry)
Returns:
boolean
true
if the entry is a descendant of ancestor
directory entry
ZipFileEntry constructor
ZipFileEntry()
ZipFileEntry#getText([options])
Get file entry content as a string
Parameters:
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)
password (string)
undefined
by default)signal (AbortSignal)
Returns:
Promise
ZipFileEntry#getBlob(mimeType [, options])
Get file entry content as a Blob
object
Parameters:
mimeType (string)
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)
password (string)
undefined
by default)signal (AbortSignal)
Returns:
Promise
ZipFileEntry#getData64URI(mimeType [, options])
Get file entry content as a data URI string
Parameters:
mimeType (string)
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)
password (string)
undefined
by default)signal (AbortSignal)
ZipFileEntry#getUint8Array([options])
Get file entry content as a
UInt8Array
object
Parameters:
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)
password (string)
undefined
by default)signal (AbortSignal)
Returns:
Promise
UInt8Array
object as resolved value
ZipFileEntry#replaceBlob(blob)
Replace the file entry content with a blob object
Parameters:
blob (Blob)
ZipFileEntry#replaceText(text)
Replace the file entry content with a string
Parameters:
text (string)
ZipFileEntry#replaceData64URI(text)
Replace the file entry content with a data URI
Parameters:
data URI (string)
ZipFileEntry#replaceUint8Array(array)
Replace the file entry content with a
UInt8Array
object
Parameters:
array (UInt8Array)
UInt8Array
object
ZipDirectoryEntry constructor
ZipDirectoryEntry()
ZipDirectoryEntry#addDirectory(name)
Add a directory into the directory entry
Parameters:
name (string)
Returns:
ZipDirectoryEntry
ZipDirectoryEntry
objectZipDirectoryEntry#addBlob(name, blob)
Add a Blob
object file into the directory
entry
Parameters:
name (string)
Returns:
ZipFileEntry
ZipFileEntry
objectZipDirectoryEntry#addText(name, text)
Add a text object into the directory entry
Parameters:
name (string)
text (string)
Returns:
ZipFileEntry
ZipFileEntry
objectZipDirectoryEntry#addData64URI(name, dataURI)
Add a data URI object into the directory entry
Parameters:
name (string)
text (string)
Returns:
ZipFileEntry
ZipFileEntry
objectZipDirectoryEntry#addUint8Array(name, array)
Add a
UInt8Array
object into the directory entry
Parameters:
name (string)
array (UInt8Array)
Returns:
ZipFileEntry
ZipFileEntry
objectZipDirectoryEntry#addHttpContent(name, URL, [, options])
Add a content retrieved from a URL into the directory entry
Parameters:
name (string)
URL (string)
options (Object)
useRangeHeader (boolean)
Range
request header should be used
(false
by default)preventHeadRequest (boolean)
HEAD
request can be sent in order the get the content
length (false
by default)forceRangeRequests (boolean)
Range
request header should be sent even if the
server does not send responses with the header Accept-Ranges: bytes
(false
by default)useXHR (boolean)
XMLHttpRequest
API instead of the fetch
API to send
requests (false
by default)useXHR
is not set to true
, you can additionnally pass any option the
fetch
API accepts.
Returns:
ZipFileEntry
ZipFileEntry
objectZipDirectoryEntry#addFileSystemEntry(fileSystemEntry)
Add a FileSystemEntry
object.
If the entry is a
FileSystemFileEntry
object then the
FileSystemFileEntry#file()
function is called in order to get its content. If the entry is a
FileSystemDirectoryEntry
object then it will ba added as a directory and all its children will be added recursively.
Parameters:
entry (FileSystemEntry)
FileSystemEntry
object to add
Returns:
Promise
ZipEntry
object as resolved value
ZipDirectoryEntry#importBlob(blob [, options])
Import a zipped content stored in a Blob
object into the directory entry
Parameters:
blob (Blob)
Blob
objectoptions (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)password (string)
filenameEncoding (string)
cp437
by
default)commentEncoding (string)
cp437
by
default)signal (AbortSignal)
Returns:
Promise
undefined
as resolved value
ZipDirectoryEntry#importData64URI(dataURI [, options])
Import a zipped content stored in a data URI string
into the directory entry
Parameters:
dataURI (string)
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)password (string)
filenameEncoding (string)
cp437
by
default)commentEncoding (string)
cp437
by
default)signal (AbortSignal)
Returns:
Promise
undefined
as resolved value
ZipDirectoryEntry#importUint8Array(array [, options])
Import a zipped content stored in a
UInt8Array
object into the directory entry
Parameters:
array (UInt8Array
)
UInt8Array
object
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)password (string)
filenameEncoding (string)
cp437
by
default)commentEncoding (string)
cp437
by
default)signal (AbortSignal)
Returns:
Promise
undefined
as resolved value
ZipDirectoryEntry#importHttpContent(URL [, options])
Import a zipped content retrieved from a URL into the directory entry
Parameters:
URL (string)
options (Object)
onprogress (Function)
index (number)
value and a max (number)
valuecheckSignature (boolean)
false
by default)password (string)
filenameEncoding (string)
cp437
by
default)commentEncoding (string)
cp437
by
default)useRangeHeader (boolean)
Range
request header should be used
(false
by default)preventHeadRequest (boolean)
HEAD
request can be sent in order the get the content
length (false
by default)forceRangeRequests (boolean)
Range
request header should be sent even if the
server does not send responses with the header Accept-Ranges: bytes
(false
by default)useXHR (boolean)
XMLHttpRequest
API instead of the fetch
API to send
requests (false
by default)signal (AbortSignal)
useXHR
is not set to true
, you can additionnally pass any option the
fetch
API accepts.
Returns:
Promise
undefined
as resolved value
ZipDirectoryEntry#exportBlob([options])
Export the zipped content in a Blob
object from the directory entry
Parameters:
options (Object)
onprogress (Function)
index (number)
value and a max (number)
value (undefined
by default)zip64 (boolean)
false
by default)level (number)
5
by default)version (number)
undefined
by default)password (string)
undefined
by default)encryptionStrength (number)
1
for AES-128, 2
for AES-192,
3
for AES-256 (3
by default)
zipCrypto (boolean)
false
by default)
relativePath (boolean)
false
by default)dataDescriptor (boolean)
true
by
default)signal (AbortSignal)
Returns:
Promise
ZipDirectoryEntry#exportData64URI([options])
Export the zipped content in a data URI from the directory entry
Parameters:
options (Object)
onprogress (Function)
index (number)
value and a max (number)
value (undefined
by default)zip64 (boolean)
false
by default)level (number)
5
by default)version (number)
undefined
by default)password (string)
undefined
by default)encryptionStrength (number)
1
for AES-128, 2
for AES-192,
3
for AES-256 (3
by default)
zipCrypto (boolean)
false
by default)
relativePath (boolean)
false
by default)dataDescriptor (boolean)
true
by
default)signal (AbortSignal)
Returns:
Promise
ZipDirectoryEntry#exportUint8Array([options])
Export the zipped content in a
UInt8Array
object from the directory entry
Parameters:
options (Object)
onprogress (Function)
index (number)
value and a max (number)
value (undefined
by default)zip64 (boolean)
false
by default)level (number)
5
by default)version (number)
undefined
by default)password (string)
undefined
by default)encryptionStrength (number)
1
for AES-128, 2
for AES-192,
3
for AES-256 (3
by default)
zipCrypto (boolean)
false
by default)
relativePath (boolean)
false
by default)dataDescriptor (boolean)
true
by
default)signal (AbortSignal)
Returns:
Promise
ZipDirectoryEntry#getChildByName(name)
Get a ZipEntry
child object given its filename
Parameters:
name (string)
Returns: