flutter_archive 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new74

flutter_archive #

Create and extract ZIP archive files. Uses Android/iOS platform APIs for high performance and optimal memory usage.

Features #

  • Supports Android and iOS.
  • Modern plugin implementation based on Kotlin (Android) and Swift (iOS).
  • Uses background processing to keep UI responsive.
  • Zip all files in a directory (optionally recursively).
  • Zip a given list of files.
  • Unzip an archive file to a given directory.

Examples #

Zip a directory #

  final dataDir = Directory("data_dir_path");
  try {
    final zipFile = File("zip_file_path");
    FlutterArchive.zipDirectory(
        sourceDir: dataDir, zipFile: zipFile, recurseSubDirs: true);
  } catch (e) {
    print(e);
  }

Zip files #

  final sourceDir = Directory("source_dir");
  final files = [
    File(sourceDir.path + "file1"),
    File(sourceDir.path + "file2")
  ];
  final zipFile = File("zip_file_path");
  try {
    FlutterArchive.zipFiles(
        sourceDir: sourceDir, files: files, zipFile: zipFile);
  } catch (e) {
    print(e);
  }

Unzip a ZIP archive #

  final zipFile = File("zip_file_path");
  final destinationDir = Directory("destination_dir_path");
  try {
    FlutterArchive.unzip(zipFile: zipFile, destinationDir: destinationDir);
  } catch (e) {
    print(e);
  }

0.1.3 #

  • [Android] Improved error handling.

0.1.2 #

  • [iOS] Zip/unzip files in a background thread to keep UI more responsive.

0.1.1+1 #

  • Minor cleanup.

0.1.1 #

  • Added support for Android V2 embedding.

0.1.0 #

  • Updated public API.
  • Improved documentation.
  • Added code samples.

0.0.2 #

  • Documented public APIs and updated README.md.

0.0.1 #

  • Initial release.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_archive/flutter_archive.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin test app'),
        ),
        body: Center(
          child: RaisedButton(
            child: Text("Test"),
            onPressed: () => _test(),
          ),
        ),
      ),
    );
  }

  final _appDataDir = Directory.systemTemp;

  final _dataFiles = {
    "file1.txt": r"abc",
    "file2.txt": r"åäö",
    "subdir1/file3.txt": r"@_£$",
    "subdir1/subdir11/file4.txt": r"123",
  };

  Future _test() async {
    print("Start test");
    var zipFile = await _testZip();
    await _testUnzip(zipFile);
    zipFile = await _testZipFiles();
    await _testUnzip(zipFile);
    print("DONE!");
  }

  Future<File> _testZip() async {
    print("_appDataDir=" + _appDataDir.path);
    final storeDir = Directory(_appDataDir.path + "/store");

    _createTestFiles(storeDir);

    final zipFile = _createZipFile("testZip.zip");
    print("Writing to zip file: " + zipFile.path);

    try {
      await FlutterArchive.zipDirectory(
          sourceDir: storeDir, zipFile: zipFile, recurseSubDirs: true);
    } on PlatformException catch (e) {
      print(e);
    }
    return zipFile;
  }

  Future<File> _testZipFiles() async {
    print("_appDataDir=" + _appDataDir.path);
    final storeDir = Directory(_appDataDir.path + "/store");

    final testFiles = _createTestFiles(storeDir);

    final zipFile = _createZipFile("testZipFiles.zip");
    print("Writing files to zip file: " + zipFile.path);

    try {
      await FlutterArchive.zipFiles(
          sourceDir: storeDir, files: testFiles, zipFile: zipFile);
    } on PlatformException catch (e) {
      print(e);
    }
    return zipFile;
  }

  Future _testUnzip(File zipFile) async {
    print("_appDataDir=" + _appDataDir.path);

    final destinationDir = Directory(_appDataDir.path + "/unzip");
    if (destinationDir.existsSync()) {
      print("Deleting existing unzip directory: " + destinationDir.path);
      destinationDir.deleteSync(recursive: true);
    }

    print("Extracting zip to directory: " + destinationDir.path);
    destinationDir.createSync();
    try {
      await FlutterArchive.unzip(
          zipFile: zipFile, destinationDir: destinationDir);
    } on PlatformException catch (e) {
      print(e);
    }

    // verify unzipped files
    _verifyFiles(destinationDir);
  }

  File _createZipFile(String fileName) {
    final zipFilePath = _appDataDir.path + "/" + fileName;
    final zipFile = File(zipFilePath);

    if (zipFile.existsSync()) {
      print("Deleting existing zip file: " + zipFile.path);
      zipFile.deleteSync();
    }
    return zipFile;
  }

  List<File> _createTestFiles(Directory storeDir) {
    if (storeDir.existsSync()) {
      storeDir.deleteSync(recursive: true);
    }
    storeDir.createSync();
    final files = <File>[];
    for (final fileName in _dataFiles.keys) {
      final file = File(storeDir.path + "/" + fileName);
      file.createSync(recursive: true);
      print("Writing file: " + file.path);
      file.writeAsStringSync(_dataFiles[fileName]);
      files.add(file);
    }

    // verify created files
    _verifyFiles(storeDir);

    return files;
  }

  void _verifyFiles(Directory filesDir) {
    print("Verifying files at: ${filesDir.path}");
    final extractedItems = filesDir.listSync(recursive: true);
    print("File count: ${extractedItems.length}");
    assert(extractedItems.whereType<File>().length == _dataFiles.length,
        "Invalid number of files");
    for (final fileName in _dataFiles.keys) {
      final file = File(filesDir.path + "/" + fileName);
      print("Verifying file: " + file.path);
      assert(file.existsSync(), "File not found: " + file.path);
      final content = file.readAsStringSync();
      assert(content == _dataFiles[fileName],
          "Invalid file content: " + file.path);
    }
    print("All files ok");
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_archive: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_archive/flutter_archive.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Feb 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test