Skip to content

Commit

Permalink
Restructured to match new pub library layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisellgren committed Oct 28, 2012
1 parent 4a81986 commit 073f4c7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 36 deletions.
6 changes: 0 additions & 6 deletions lib/central_directory.dart → lib/src/central_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
* http://www.opensource.org/licenses/mit-license.php
*/

library CentralDirectory;

import 'zip.dart';
import 'central_directory_file_header.dart';
import 'util.dart';

/**
* Creates a new instance of the Central Directory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
* http://www.opensource.org/licenses/mit-license.php
*/

library CentralDirectoryFileHeader;

import 'zip.dart';
import 'util.dart';
import 'local_file_header.dart';
import 'dart:utf';

/**
* Creates a new instance of the Central Directory File Header.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
* http://www.opensource.org/licenses/mit-license.php
*/

library EndOfCentralDirectoryRecord;

import 'zip.dart';
import 'util.dart';

/**
* Creates a new instance of the End of Central Directory Record.
*/
Expand Down
6 changes: 0 additions & 6 deletions lib/local_file_header.dart → lib/src/local_file_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
* http://www.opensource.org/licenses/mit-license.php
*/

library LocalFileHeader;

import 'zip.dart';
import 'util.dart';
import 'dart:utf';

class LocalFileHeader {
List<int> content;

Expand Down
File renamed without changes.
57 changes: 48 additions & 9 deletions lib/zip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
library zip;

import 'dart:io';
import 'end_of_central_directory_record.dart';
import 'central_directory.dart';
import 'central_directory_file_header.dart';
import 'local_file_header.dart';
import 'util.dart';
import 'dart:utf';
import 'src/util.dart';
import 'package:crc32/crc32.dart';

part 'src/end_of_central_directory_record.dart';
part 'src/central_directory.dart';
part 'src/central_directory_file_header.dart';
part 'src/local_file_header.dart';

/**
* This class represents a Zip file.
*/
Expand All @@ -32,6 +34,9 @@ class Zip {
EndOfCentralDirectoryRecord _endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();
CentralDirectory _centralDirectory = new CentralDirectory();

// Specifies how many files are being added to the Zip, but are still waiting to be read.
int _filesPendingAdditionCount = 0;

Zip(path) {
if (path is String)
_filePath = new Path(path);
Expand All @@ -42,7 +47,8 @@ class Zip {
/**
* Saves the Zip archive.
*/
void save() {
Future save() {
var completer = new Completer();
_file = new File.fromPath(this._filePath);

_file.create().then((File file) {
Expand Down Expand Up @@ -77,9 +83,11 @@ class Zip {
final buffer = _endOfCentralDirectoryRecord.save();
raf.writeListSync(buffer, 0, buffer.length);

// TODO: Done saving, do something.
completer.complete(null);
});
});

return completer.future;
}

/**
Expand Down Expand Up @@ -113,10 +121,21 @@ class Zip {

/**
* Adds the data associated with the given filename to the Zip.
*
* The filename must be a full path. Folders will be generated for you.
*/
void addFileFromString(String filename, String data) {
this.addFileFromBytes(filename, data.charCodes());
}

/**
* Adds the data associated with the given filename to the Zip.
*
* The filename must be a full path. Folders will be generated for you.
*/
void addFileFromString(filename, String data) {
void addFileFromBytes(String filename, List<int> data) {
final fh = new LocalFileHeader();
fh.content = data.charCodes();
fh.content = data;
fh.crc32 = CRC32.compute(fh.content);
fh.uncompressedSize = data.length;
fh.compressedSize = fh.uncompressedSize;
Expand All @@ -128,6 +147,26 @@ class Zip {
_endOfCentralDirectoryRecord.totalCentralDirectoryEntries += 1;
}

/**
* Adds the contents of the specified file associated with the given filename.
*
* The filename must be a full path. Folders will be generated for you.
*
* For example: zip.addFileFromPath('myfile.txt', new Path('path/to/file.txt'));
*/
Future addFileFromPath(String filename, Path path) {
var completer = new Completer();
_filesPendingAdditionCount += 1;

new File.fromPath(path).readAsBytes().then((List<int> data) {
addFileFromBytes(filename, data);
_filesPendingAdditionCount -= 1;
completer.complete(null);
});

return completer.future;
}

/**
* Extracts the entire archive to the given path.
*/
Expand Down
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ description: A zip parser, reader and generator written in pure Dart. It is memo
author: Kai Sellgren <kaisellgren@gmail.com>
homepage: http://github.com/kaisellgren/DartZip
dependencies:
crc32:
git: git://github.com/kaisellgren/CRC32.git
unittest:
sdk: unittest
crc32:
git: git://github.com/kaisellgren/CRC32.git
2 changes: 1 addition & 1 deletion test/test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../lib/zip.dart';
import 'package:zip/zip.dart';
import 'dart:io';

void main() {
Expand Down
Binary file removed test/test.zip
Binary file not shown.

0 comments on commit 073f4c7

Please sign in to comment.