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.
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() {
final inputStream = InputFileStream('test.zip');
final archive = ZipDecoder().decodeStream(inputStream);
final symbolicLinks = [];
for (final file in archive) {
if (file.isSymbolicLink) {
symbolicLinks.add(file);
continue;
}
if (file.isFile) {
final outputStream = OutputFileStream('out/${file.name}');
entity.writeContent(outputStream);
outputStream.closeSync();
} else {
Directory('out/${file.name}').createSync(recursive: true);
}
}
for (final entity in symbolicLinks) {
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';
final inputStream = InputFileStream('test.zip');
final archive = ZipDecoder().decodeStream(inputStream);
extractArchiveToDisk(archive, 'out');