搜尋分類、套件與程式碼儲存庫

套件詳情v4.5.2

archive

提供各種歸檔和壓縮格式(例如 zip、tar、bzip2、gzip 和 zlib)的編碼器和解碼器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1910萬
喜歡數
906
Pub 分數
160

安裝套件

flutter pub add archive

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

該歸檔套件為多種歸檔與壓縮格式(包含 ZIP、TAR、BZIP2、GZIP 與 ZLIB)提供高效之編碼器與解碼器。它讓 Flutter 應用程式能在所有主要平台——Android、iOS、Web、macOS、Windows 與 Linux 上讀取、寫入與操作壓縮檔案。適用於處理檔案群組、備份或減小資料傳輸體積,支援串流與記憶體中作業。此套件輕量、文件完整,並可透過 pub.dev 無縫整合至 Flutter 專案。

適用情境

  • 處理 ZIP 檔案的解壓和建立
  • 壓縮應用程式資料以進行儲存
  • 串流壓縮檔案處理
  • 跨平台歸檔操作
  • 備份和還原功能

主要特色

  • 支援 ZIP、TAR、GZIP、BZIP2 和 ZLIB
  • 跨平台相容性
  • 基於流和記憶體中的操作
  • 輕量且快速的效能
  • 用於編碼和解碼的簡單 API

適合對象

  • 需要文件壓縮工具的開發人員
  • 在多個平台上需要歸檔處理的應用程式
  • 數據打包和傳輸場景
  • 離線資料儲存優化

使用建議

  • 僅限標準壓縮格式
  • 沒有內建的歸檔管理圖形使用者介面
  • 處理大檔案時需要仔細的記憶體管理
  • 不適用於大規模檔案的即時串流傳輸

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

archive

Dart CI pub package

4.0 Update

The Archive library was originally written when the web was the primary use of Dart. File IO was less of a concern and the design was around having everything in memory. As other uses of Dart came about, such as Flutter, a lot of File IO operations were added to the library, but not in a very clean way.

The design goal for the 4.0 revision of the library is to ensure File IO is a primary focus, while minimizing memory usage. Memory-only interfaces are still available for web platforms.

Migrating 3.x to 4.x.
Migration quick tips:
  • decodeBuffer has been renamed to decodeStream in the various decoder classes.
  • InputStream has been renamed to InputMemoryStream.
  • OutputStream has been renamed to OutputMemoryStream.

Overview

A Dart library to encode and decode various archive and compression formats.

The archive library currently supports the following codecs:

  • Zip
  • Tar
  • ZLib
  • GZip
  • BZip2
  • XZ

Usage

package:archive/archive.dart

  • Can be used for both web and native applications.

package:archive/archive_io.dart

  • Provides some extra utilities for 'dart:io' based applications.
Decoding a zip file in memory
import 'package:archive/archive.dart';
import 'dart:io';
void main() {
  final bytes = File('test.zip').readAsBytesSync();
  final archive = ZipDecoder().decodeBytes(bytes);
  for (final entry in archive) {
    if (entry.isFile) {
      final fileBytes = file.readBytes();
      File('out/${file.fullPathName}')
        ..createSync(recursive: true)
        ..writeAsBytesSync(fileBytes);
    }
  }
}
Using InputFileStream and OutputFileStream to extract a zip:
import 'dart:io';
import 'package:archive/archive.dart';
void main() {
  // Use an InputFileStream to access the zip file without storing it in memory.
  // Note that using InputFileStream will result in an error from the web platform  
  // as there is no file system there.
  final inputStream = InputFileStream('test.zip');
  // Decode the zip from the InputFileStream. The archive will have the contents of the
  // zip, without having stored the data in memory. 
  final archive = ZipDecoder().decodeStream(inputStream);
  final symbolicLinks = []; // keep a list of the symbolic link entities, if any.
  // For all of the entries in the archive
  for (final file in archive) {
    // You should create symbolic links **after** the rest of the archive has been
    // extracted, otherwise the file being linked might not exist yet.
    if (file.isSymbolicLink) {
      symbolicLinks.add(file);
      continue;
    }
    if (file.isFile) {
      // Write the file content to a directory called 'out'.
      // In practice, you should make sure file.name doesn't include '..' paths
      // that would put it outside of the extraction directory.
      // An OutputFileStream will write the data to disk.
      final outputStream = OutputFileStream('out/${file.name}');
      // The writeContent method will decompress the file content directly to disk without
      // storing the decompressed data in memory. 
      entity.writeContent(outputStream);
      // Make sure to close the output stream so the File is closed.
      outputStream.closeSync();
    } else {
      // If the entity is a directory, create it. Normally writing a file will create
      // the directories necessary, but sometimes an archive will have an empty directory
      // with no files.
      Directory('out/${file.name}').createSync(recursive: true);
    }
  }
  // Create symbolic links **after** the rest of the archive has been extracted to make sure
  // the file being linked exists.
  for (final entity in symbolicLinks) {
    // Before using this in production code, you should ensure the symbolicLink path
    // points to a file within the archive, otherwise it could be a security issue.
    final link = Link('out/${entity.fullPathName}');
    link.createSync(entity.symbolicLink!, recursive: true);
  }
}
extractFileToDisk

extractFileToDisk is a convenience function to extract the contents of an archive file directory to an output directory. The type of archive it is will be determined by the file extension.

import 'package:archive/archive_io.dart';
// ...
extractFileToDisk('test.zip', 'out');
extractArchiveToDisk

extractArchiveToDisk is a convenience function to write the contents of an Archive to an output directory.

import 'package:archive/archive_io.dart';
// ...
// Use an InputFileStream to access the zip file without storing it in memory.
final inputStream = InputFileStream('test.zip');
// Decode the zip from the InputFileStream. The archive will have the contents of the
// zip, without having stored the data in memory. 
final archive = ZipDecoder().decodeStream(inputStream);
extractArchiveToDisk(archive, 'out');

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

所在榜單