diff --git a/lib/Util.dart b/lib/Util.dart index 703601a..5065be9 100644 --- a/lib/Util.dart +++ b/lib/Util.dart @@ -1,4 +1,6 @@ -#library('Util'); +library Util; + +import 'dart:math'; /** * Converts the byte sequence to a numeric representation. @@ -7,7 +9,7 @@ bytesToValue(List bytes) { var value = 0; for (var i = 0, length = bytes.length; i < length; i++) { - value += bytes[i] * Math.pow(256, i); + value += bytes[i] * pow(256, i); } return value; diff --git a/lib/Zip.dart b/lib/Zip.dart index a981fb5..77fefc7 100644 --- a/lib/Zip.dart +++ b/lib/Zip.dart @@ -6,12 +6,12 @@ * http://www.opensource.org/licenses/mit-license.php */ -#library('Zip'); +library Zip; -#import('dart:io'); -#import('EndOfCentralDirectoryRecord.dart'); -#import('CentralDirectory.dart'); -#import('Util.dart'); +import 'dart:io'; +import 'end_of_central_directory_record.dart'; +import 'central_directory.dart'; +import 'util.dart'; /** * This class represents a Zip file. @@ -30,6 +30,7 @@ class Zip { EndOfCentralDirectoryRecord _endOfCentralDirectoryRecord; CentralDirectory _centralDirectory; List files; + bool _initialized = false; Zip(Path this._filePath); @@ -40,6 +41,7 @@ class Zip { */ Future open() { var completer = new Completer(); + _initialized = true; this._file = new File.fromPath(this._filePath); @@ -48,7 +50,7 @@ class Zip { try { this._process(); - } catch (Exception e) { + } on Exception catch (e) { completer.completeException(e); } @@ -90,7 +92,7 @@ class Zip { } // If the Zip is not yet opened, open it first before we can extract it. - if (this._centralDirectory == null) { + if (this._initialized == false) { this.open().then((error) { // Check for potential errors. diff --git a/lib/CentralDirectory.dart b/lib/central_directory.dart similarity index 76% rename from lib/CentralDirectory.dart rename to lib/central_directory.dart index c065e39..f0d99b5 100644 --- a/lib/CentralDirectory.dart +++ b/lib/central_directory.dart @@ -6,11 +6,11 @@ * http://www.opensource.org/licenses/mit-license.php */ -#library('CentralDirectory'); +library CentralDirectory; -#import('Zip.dart'); -#import('CentralDirectoryFileHeader.dart'); -#import('Util.dart'); +import 'zip.dart'; +import 'central_directory_file_header.dart'; +import 'util.dart'; /** * Creates a new instance of the Central Directory. @@ -65,22 +65,22 @@ class CentralDirectory { // file comment (variable size) var position = 0; - var signatureSize = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE.length; - var signatureCodes = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE.charCodes(); + final signatureSize = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE.length; + final signatureCodes = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE.charCodes(); // Create file headers. Loop until we have gone through the entire buffer. while (true) { // Calculate sizes for dynamic parts. - var filenameSize = bytesToValue(this._chunk.getRange(28, 2)); - var extraFieldSize = bytesToValue(this._chunk.getRange(30, 2)); - var fileCommentSize = bytesToValue(this._chunk.getRange(32, 2)); + final filenameSize = bytesToValue(this._chunk.getRange(28, 2)); + final extraFieldSize = bytesToValue(this._chunk.getRange(30, 2)); + final fileCommentSize = bytesToValue(this._chunk.getRange(32, 2)); - var dynamicSize = filenameSize + fileCommentSize + extraFieldSize; - var totalFileHeaderSize = dynamicSize + FILE_HEADER_STATIC_SIZE; + final dynamicSize = filenameSize + fileCommentSize + extraFieldSize; + final totalFileHeaderSize = dynamicSize + FILE_HEADER_STATIC_SIZE; // Push a new file header. if (this._chunk.length >= position + totalFileHeaderSize) { - var buffer = this._chunk.getRange(position, totalFileHeaderSize); + final buffer = this._chunk.getRange(position, totalFileHeaderSize); this.fileHeaders.add(new CentralDirectoryFileHeader(buffer, this._data)); // Move the position pointer forward. diff --git a/lib/CentralDirectoryFileHeader.dart b/lib/central_directory_file_header.dart similarity index 92% rename from lib/CentralDirectoryFileHeader.dart rename to lib/central_directory_file_header.dart index ba626e1..6e08334 100644 --- a/lib/CentralDirectoryFileHeader.dart +++ b/lib/central_directory_file_header.dart @@ -6,12 +6,12 @@ * http://www.opensource.org/licenses/mit-license.php */ -#library('CentralDirectoryFileHeader'); +library CentralDirectoryFileHeader; -#import('Zip.dart'); -#import('Util.dart'); -#import('LocalFileHeader.dart'); -#import('dart:utf'); +import 'zip.dart'; +import 'util.dart'; +import 'local_file_header.dart'; +import 'dart:utf'; /** * Creates a new instance of the Central Directory File Header. @@ -93,7 +93,6 @@ class CentralDirectoryFileHeader { this.fileComment = this._chunk.getRange(46 + this.filenameLength + this.extraFieldLength, this.fileCommentLength); // TODO: Are there scenarios where LocalFileHeader.compressedSize != CentralDirectoryFileHeader.compressedSize? - print("offset ${this.localHeaderOffset}"); this.localFileHeader = new LocalFileHeader(this._data.getRange(this.localHeaderOffset, this._data.length - this.localHeaderOffset)); } } \ No newline at end of file diff --git a/lib/EndOfCentralDirectoryRecord.dart b/lib/end_of_central_directory_record.dart similarity index 92% rename from lib/EndOfCentralDirectoryRecord.dart rename to lib/end_of_central_directory_record.dart index be798b3..19b5e5f 100644 --- a/lib/EndOfCentralDirectoryRecord.dart +++ b/lib/end_of_central_directory_record.dart @@ -6,10 +6,10 @@ * http://www.opensource.org/licenses/mit-license.php */ -#library('EndOfCentralDirectoryRecord'); +library EndOfCentralDirectoryRecord; -#import('Zip.dart'); -#import('Util.dart'); +import 'zip.dart'; +import 'util.dart'; /** * Creates a new instance of the End of Central Directory Record. diff --git a/lib/LocalFileHeader.dart b/lib/local_file_header.dart similarity index 93% rename from lib/LocalFileHeader.dart rename to lib/local_file_header.dart index 681f0d2..e7a05cd 100644 --- a/lib/LocalFileHeader.dart +++ b/lib/local_file_header.dart @@ -6,11 +6,11 @@ * http://www.opensource.org/licenses/mit-license.php */ -#library('LocalFileHeader'); +library LocalFileHeader; -#import('Zip.dart'); -#import('Util.dart'); -#import('dart:utf'); +import 'zip.dart'; +import 'util.dart'; +import 'dart:utf'; class LocalFileHeader { List _chunk; diff --git a/test/test.dart b/test/test.dart index dd3a00e..594ee6a 100644 --- a/test/test.dart +++ b/test/test.dart @@ -1,8 +1,10 @@ -#import('../lib/Zip.dart'); -#import('dart:io'); +import '../lib/zip.dart'; +import 'dart:io'; void main() { - var zip = new Zip(new Path('test.zip')); + final zip = new Zip(new Path('test.zip')); + //zip.addFileFromString('something.txt', 'content goes here'); + //zip.save(); zip.extractTo(new Path.fromNative("${new Directory.current().path}/test-extraction/"));