跳过内容

开始使用

安装

安装
终端窗口
bun add @capgo/capacitor-zip
bunx cap sync

导入

导入
import { CapacitorZip } from '@capgo/capacitor-zip';

API概述

API概述

压缩文件或目录以创建 ZIP 档案

从源文件或目录创建压缩存档。存档将包含整个目录结构,如果源是文件夹。

平台相关说明:

  • iOS:不支持密码保护。如果提供密码,它将被忽略并记录一个警告。
  • Android:当提供密码时支持AES-256加密。
  • Web:不支持。调用时会抛出错误。
import { CapacitorZip } from '@capgo/capacitor-zip';
// Compress a directory without password
await CapacitorZip.zip({
source: '/path/to/my-folder',
destination: '/path/to/output.zip'
});

从指定的目的目录中解压一个ZIP存档。

从ZIP存档中提取所有文件和文件夹,同时保留目录结构。创建目的目录如果它不存在。

平台相关说明:

  • iOS:支持标准ZIP存档。密码保护的存档将使用提供的密码进行提取。
  • Android:支持带密码的AES加密存档。包括zip slip漏洞保护。
  • Web: 下载每个文件到浏览器的下载文件夹。无法创建目录结构。
import { CapacitorZip } from '@capgo/capacitor-zip';
// Extract a standard ZIP archive
await CapacitorZip.unzip({
source: '/path/to/archive.zip',
destination: '/path/to/extract-folder'
});

类型参考

类型参考

ZipOptions

压缩选项

复制到剪贴板

export interface ZipOptions {
/**
* Path to the file or directory to compress.
*
* This can be an absolute path or a path relative to the app's working directory.
* If the source is a directory, all its contents will be recursively compressed
* while preserving the directory structure.
*
* Platform-specific notes:
* - iOS: Use file:// URLs or absolute paths. Relative paths are resolved from the app's documents directory.
* - Android: Use absolute file paths or content:// URIs for files accessible via the Android Storage Access Framework.
* - Web: Not supported.
*
* @since 7.0.0
* @example '/Users/app/Documents/my-folder'
* @example '/var/mobile/Containers/Data/Application/.../Documents/file.pdf'
* @example 'file:///storage/emulated/0/Download/document.pdf'
*/
source: string;
/**
* Path where the ZIP archive will be created.
*
* The destination path must include the .zip file extension. If the parent
* directory doesn't exist, it will be created automatically.
*
* Platform-specific notes:
* - iOS: Use file:// URLs or absolute paths. Relative paths are resolved from the app's documents directory.
* - Android: Use absolute file paths. The plugin will create any missing parent directories.
* - Web: Not supported.
*
* @since 7.0.0
* @example '/Users/app/Documents/archive.zip'
* @example '/var/mobile/Containers/Data/Application/.../Documents/backup.zip'
* @example 'file:///storage/emulated/0/Download/compressed.zip'
*/
destination: string;
/**
* Optional password for encrypting the ZIP archive.
*
* When provided, the archive will be encrypted and require this password
* to extract. Uses AES-256 encryption on Android.
*
* Platform-specific notes:
* - iOS: Password protection is NOT supported. The password will be ignored and a warning will be logged.
* - Android: Supports AES-256 encryption via zip4j library. The password must be provided during extraction.
* - Web: Not supported.
*
* @since 7.0.0
* @example 'mySecurePassword123'
*/
password?: string;
/**
* Whether to include the parent folder in the ZIP archive.
*
* When true (default), the source folder itself becomes the root directory in the archive.
* When false, only the contents of the source folder are included at the root level.
*
* This option only applies when the source is a directory. For single files, this option is ignored.
*
* @default true
* @since 8.0.5
* @example
* ```typescript
* // With includeParentFolder: true (default)
* // Source: /cache/temp/ containing [database.backup, media/]
* // ZIP contains: temp/database.backup, temp/media/
* await CapacitorZip.zip({
* source: '/cache/temp',
* destination: '/cache/backup.zip',
* includeParentFolder: true
* });
* ```
* @example
* ```typescript
* // With includeParentFolder: false
* // Source: /cache/temp/ containing [database.backup, media/]
* // ZIP contains: database.backup, media/
* await CapacitorZip.zip({
* source: '/cache/temp',
* destination: '/cache/backup.zip',
* includeParentFolder: false
* });
* ```
*/
includeParentFolder?: boolean;
}

UnzipOptions

复制到剪贴板

真实来源

export interface UnzipOptions {
/**
* Path to the ZIP archive to extract.
*
* The source must be a valid ZIP file. If the file doesn't exist or is
* corrupted, the operation will fail with an error.
*
* Platform-specific notes:
* - iOS: Use file:// URLs or absolute paths. Relative paths are resolved from the app's documents directory.
* - Android: Use absolute file paths or content:// URIs for files accessible via the Android Storage Access Framework.
* - Web: Use HTTP/HTTPS URLs. The file will be fetched and extracted in the browser.
*
* @since 7.0.0
* @example '/Users/app/Documents/archive.zip'
* @example '/var/mobile/Containers/Data/Application/.../Documents/backup.zip'
* @example 'file:///storage/emulated/0/Download/compressed.zip'
* @example 'https://example.com/files/archive.zip' (Web only)
*/
source: string;
/**
* Path to the directory where files will be extracted.
*
* The destination directory will be created if it doesn't exist. All files
* and folders from the archive will be extracted while preserving the
* directory structure.
*
* Platform-specific notes:
* - iOS: Use file:// URLs or absolute paths. Relative paths are resolved from the app's documents directory.
* - Android: Use absolute file paths. Includes protection against zip slip vulnerabilities.
* - Web: Not applicable. Files are downloaded individually to the browser's download folder.
*
* @since 7.0.0
* @example '/Users/app/Documents/extracted'
* @example '/var/mobile/Containers/Data/Application/.../Documents/files'
* @example 'file:///storage/emulated/0/Download/extracted-files'
*/
destination: string;
/**
* Optional password for decrypting password-protected archives.
*
* Required if the ZIP archive was encrypted with a password. If the password
* is incorrect, extraction will fail with an error.
*
* Platform-specific notes:
* - iOS: Supports password-protected ZIP archives.
* - Android: Supports AES-encrypted archives created with zip4j or standard password-protected ZIPs.
* - Web: Not supported. Password-protected archives cannot be extracted in the browser.
*
* @since 7.0.0
* @example 'mySecurePassword123'
*/
password?: string;
}

__CAPGO_KEEP_0__

__CAPGO_KEEP_1__

本页面由插件生成 src/definitions.ts当上游的公共API发生变化时,请重新同步