아카이브 패키지는 ZIP, TAR, BZIP2, GZIP, ZLIB 등 다양한 아카이브 및 압축 형식에 대해 효율적인 인코더와 디코더를 제공합니다. 이는 Flutter 애플리케이션에서 주요 플랫폼인 Android, iOS, Web, macOS, Windows, Linux에서 압축 파일을 읽고 쓰며 조작할 수 있도록 해줍니다. 파일 번들, 백업 또는 크기가 작아진 데이터 전송 처리에 적합하며 스트리밍 및 메모리 내 작업을 모두 지원합니다. 이 패키지는 가볍고 잘 문서화되어 있으며 pub.dev를 통해 Flutter 프로젝트에 원활하게 통합됩니다.
사용 사례
ZIP 파일 추출 및 생성 처리
저장을 위해 앱 데이터를 압축 중
스트리밍 압축 파일 처리
크로스 플랫폼 아카이브 조작
백업 및 복원 기능
주요 기능
ZIP, TAR, GZIP, BZIP2 및 ZLIB 지원
크로스 플랫폼 호환성
스트림 기반 및 메모리 내 연산
경량이고 빠른 성능
인코딩 및 디코딩을 위한 간단한 API
적합한 대상
파일 압축 도구가 필요한 개발자
다중 플랫폼에서 아카이브 처리가 필요한 앱
데이터 패키징 및 전송 시나리오
오프라인 데이터 저장 최적화
고려 사항
표준 압축 형식에 한정
압축 파일 관리용 내장 GUI 없음
큰 파일과 함께 사용할 때 메모리 처리에 주의가 필요합니다
거대한 아카이브의 실시간 스트리밍을 위한 설계가 아님
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
archive
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.
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';
voidmain() {
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';
voidmain() {
// 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 archivefor (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.
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');