zip.dart 3.3 KB
Newer Older
Devon Carew's avatar
Devon Carew committed
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert' show UTF8;
6
import 'dart:io';
Devon Carew's avatar
Devon Carew committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

import 'package:archive/archive.dart';
import 'package:path/path.dart' as path;

import 'base/process.dart';

abstract class ZipBuilder {
  factory ZipBuilder() {
    if (exitsHappy(<String>['which', 'zip'])) {
      return new _ZipToolBuilder();
    } else {
      return new _ArchiveZipBuilder();
    }
  }

  ZipBuilder._();

  List<ZipEntry> entries = <ZipEntry>[];

  void addEntry(ZipEntry entry) => entries.add(entry);

  void createZip(File outFile, Directory zipBuildDir);
}

class ZipEntry {
  ZipEntry.fromFile(this.archivePath, File file) {
    this._file = file;
  }

  ZipEntry.fromString(this.archivePath, String contents) {
    this._contents = contents;
  }

  final String archivePath;

  File _file;
  String _contents;

  bool get isStringEntry => _contents != null;
}

class _ArchiveZipBuilder extends ZipBuilder {
  _ArchiveZipBuilder() : super._();

51
  @override
Devon Carew's avatar
Devon Carew committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  void createZip(File outFile, Directory zipBuildDir) {
    Archive archive = new Archive();

    for (ZipEntry entry in entries) {
      if (entry.isStringEntry) {
        List<int> data = UTF8.encode(entry._contents);
        archive.addFile(new ArchiveFile.noCompress(entry.archivePath, data.length, data));
      } else {
        List<int> data = entry._file.readAsBytesSync();
        archive.addFile(new ArchiveFile(entry.archivePath, data.length, data));
      }
    }

    List<int> zipData = new ZipEncoder().encode(archive);
    outFile.writeAsBytesSync(zipData);
  }
}

class _ZipToolBuilder extends ZipBuilder {
  _ZipToolBuilder() : super._();

73
  @override
Devon Carew's avatar
Devon Carew committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  void createZip(File outFile, Directory zipBuildDir) {
    if (outFile.existsSync())
      outFile.deleteSync();

    if (zipBuildDir.existsSync())
      zipBuildDir.deleteSync(recursive: true);
    zipBuildDir.createSync(recursive: true);

    for (ZipEntry entry in entries) {
      if (entry.isStringEntry) {
        List<int> data = UTF8.encode(entry._contents);
        File file = new File(path.join(zipBuildDir.path, entry.archivePath));
        file.parent.createSync(recursive: true);
        file.writeAsBytesSync(data);
      } else {
        List<int> data = entry._file.readAsBytesSync();
        File file = new File(path.join(zipBuildDir.path, entry.archivePath));
        file.parent.createSync(recursive: true);
        file.writeAsBytesSync(data);
      }
    }

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if (_getCompressedNames().isNotEmpty) {
      runCheckedSync(
        <String>['zip', '-q', outFile.absolute.path]..addAll(_getCompressedNames()),
        workingDirectory: zipBuildDir.path,
        truncateCommand: true
      );
    }

    if (_getStoredNames().isNotEmpty) {
      runCheckedSync(
        <String>['zip', '-q', '-0', outFile.absolute.path]..addAll(_getStoredNames()),
        workingDirectory: zipBuildDir.path,
        truncateCommand: true
      );
    }
Devon Carew's avatar
Devon Carew committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
  }

  Iterable<String> _getCompressedNames() {
    return entries
      .where((ZipEntry entry) => !entry.isStringEntry)
      .map((ZipEntry entry) => entry.archivePath);
  }

  Iterable<String> _getStoredNames() {
    return entries
      .where((ZipEntry entry) => entry.isStringEntry)
      .map((ZipEntry entry) => entry.archivePath);
  }
}