搜索插件、代码仓与 APP 分类

插件详情v4.7.1

archive

提供用于各种归档和压缩格式(如 zip、tar、bzip2、gzip 和 zlib)的编码器和解码器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2070万
喜欢数
909
Pub 分数
150

安装插件

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 项

源码仓库

使用此插件的项目

所在榜单