Skip to content

Zip

The Zip class provides methods for working with ZIP archives, including creating, extracting, and managing files within ZIP archives. It supports both synchronous and asynchronous operations for handling ZIP files.

Constructor:

  • constructor(zipPathOrFile: string | DocumentFile,
    password?: string)
  • zipPathOrFile: The path to the ZIP file or a DocumentFile object representing the ZIP file.
  • password: (Optional) The password for the ZIP file if it is encrypted. Defaults to an empty string if not provided.

Properties:

  • fileNames: string[] (read-only)
    An array of file names contained in the ZIP archive.

Static Methods:

  • extractAll(zipPathOrFile: string | DocumentFile, targetPathOrFile: string | DocumentFile)
    Extracts all files from the specified ZIP archive to the target directory or file.
  • zipPathOrFile: The path to the ZIP file or a DocumentFile object representing the ZIP file.
  • targetPathOrFile: The path to the target directory or file where the contents will be extracted.
  • extractAllAsync(zipPathOrFile: string | DocumentFile, targetPathOrFile: string | DocumentFile): Promise<void>
    Asynchronously extracts all files from the specified ZIP archive to the target directory or file.
  • zipPathOrFile: The path to the ZIP file or a DocumentFile object representing the ZIP file.
  • targetPathOrFile: The path to the target directory or file where the contents will be extracted.
  • zipDir(sourcePathOrFileOrDir: string | DocumentFile, zipPathOrFile: string | DocumentFile)
    Creates a ZIP archive from the specified directory or file.
  • sourcePathOrFileOrDir: The path to the directory or file to be zipped, or a DocumentFile object representing the source.
  • zipPathOrFile: The path where the ZIP file will be created or a DocumentFile object representing the destination ZIP file.
  • zipDirAsync(sourcePathOrFileOrDir: string | DocumentFile, zipPathOrFile: string | DocumentFile): Promise<void>
    Asynchronously creates a ZIP archive from the specified directory or file.
  • sourcePathOrFileOrDir: The path to the directory or file to be zipped, or a DocumentFile object representing the source.
  • zipPathOrFile: The path where the ZIP file will be created or a DocumentFile object representing the destination ZIP file.

Instance Methods:

  • open(write?: boolean)
    Opens the ZIP archive. If write is true, the ZIP file is opened in write mode; otherwise, it is opened in read mode. Default is false (read mode).
  • close()
    Closes the ZIP archive, finalizing any changes if it was opened in write mode.
  • add(fileName: string, pathDataOrFile: string | DocumentFile)
    Adds a file to the ZIP archive.
  • fileName: The name of the file to be added to the ZIP archive.
  • pathDataOrFile: The path to the file or a DocumentFile object representing the file to be added.
  • addAsync(fileName: string,
    pathDataOrFile: string | DocumentFile): Promise<void>
    Asynchronously adds a file to the ZIP archive.
  • fileName: The name of the file to be added to the ZIP archive.
  • pathDataOrFile: The path to the file or a DocumentFile object representing the file to be added.
  • extract(fileName: string,
    targetPathOrFile?: string | DocumentFile
    Extracts a specific file from the ZIP archive.
  • fileName: The name of the file to be extracted.
  • targetPathOrFile: (Optional) The path or DocumentFile object where the extracted file will be saved. If not provided, the file is extracted to the current directory.
  • extractAsync(fileName: string, targetPathOrFile?: string | DocumentFile): Promise<void>
    Asynchronously extracts a specific file from the ZIP archive.
  • fileName: The name of the file to be extracted.
  • targetPathOrFile: (Optional) The path or DocumentFile object where the extracted file will be saved. If not provided, the file is extracted to the current directory.
  • deflate(buffer: Uint8Array): Uint8Array
    Compresses the given buffer using the deflate algorithm.
  • buffer: The data to be compressed as a Uint8Array.
  • deflateAsync(buffer: Uint8Array): Promise<Uint8Array>
    Asynchronously compresses the given buffer using the deflate algorithm.
  • buffer: The data to be compressed as a Uint8Array.
  • inflate(buffer: Uint8Array): Uint8Array
    Decompresses the given buffer using the inflate algorithm.
  • buffer: The compressed data as a Uint8Array.
  • inflateAsync(buffer: Uint8Array): Promise<Uint8Array>
    Asynchronously decompresses the given buffer using the inflate algorithm.
  • buffer: The compressed data as a Uint8Array.

Example 1:

// Create a new Zip instance  
const zip = new Zip('path/to/archive.zip', 'password123');

// Open the ZIP file for writing  
zip.open(true);

// Add a file to the ZIP archive  
zip.add('file.txt', 'path/to/file.txt');

// Asynchronously add a file to the ZIP archive  
zip.addAsync('newFile.txt', 'path/to/newFile.txt').then(()=> {  
    console.log('File added successfully');  
});

// Close the ZIP file  
zip.close();

Example 2:

// Extract all files from the ZIP archive to a target directory  
Zip.extractAll('path/to/archive.zip', 'path/to/target/dir');

Example 3:

// Deflate data  
const compressed = zip.deflate(new Uint8Array([1, 2, 3, 4]));  

Example 4:

// Inflate data  
const decompressed = zip.inflate(compressed);