diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..21fe048 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2012 Kai Sellgren + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..79054a2 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +DartZip +== +A Zib library written in Dart. + +## License +The library is licensed under MIT. \ No newline at end of file diff --git a/lib/CentralDirectory.dart b/lib/CentralDirectory.dart new file mode 100644 index 0000000..25b590a --- /dev/null +++ b/lib/CentralDirectory.dart @@ -0,0 +1,102 @@ +/*! + * DartZip + * + * Copyright (C) 2012, Kai Sellgren + * Licensed under the MIT License. + * http://www.opensource.org/licenses/mit-license.php + */ + +#library('CentralDirectory'); + +#import('Zip.dart'); +#import('CentralDirectoryFileHeader.dart'); +#import('Util.dart'); + +/** + * Creates a new instance of the Central Directory. + */ +class CentralDirectory { + List _data; + + static final FILE_HEADER_STATIC_SIZE = 46; // The static size of the file header. + + var signature = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE; + List fileHeaders; + var digitalSignature; + + CentralDirectory(List data) { + this._data = data; + this.fileHeaders = []; + + this._process(); + } + + /** + * Reads the data and sets the information to class members. + */ + void _process() { + // [file header 1] + // . + // . + // . + // [file header n] + // [digital signature] + // + // File header: + // + // central file header signature 4 bytes (0x02014b50) + // version made by 2 bytes + // version needed to extract 2 bytes + // general purpose bit flag 2 bytes + // compression method 2 bytes + // last mod file time 2 bytes + // last mod file date 2 bytes + // crc-32 4 bytes + // compressed size 4 bytes + // uncompressed size 4 bytes + // file name length 2 bytes + // extra field length 2 bytes + // file comment length 2 bytes + // disk number start 2 bytes + // internal file attributes 2 bytes + // external file attributes 4 bytes + // relative offset of local header 4 bytes + // + // file name (variable size) + // extra field (variable size) + // 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(); + + // Create file headers. Loop until we have gone through the entire buffer. + while (true) { + // Calculate sizes for dynamic parts. + var filenameSize = bytesToValue(this._data.getRange(28, 2)); + var extraFieldSize = bytesToValue(this._data.getRange(30, 2)); + var fileCommentSize = bytesToValue(this._data.getRange(32, 2)); + + var dynamicSize = filenameSize + fileCommentSize + extraFieldSize; + var totalFileHeaderSize = dynamicSize + FILE_HEADER_STATIC_SIZE; + + // Push a new file header. + if (this._data.length >= position + totalFileHeaderSize) { + var buffer = this._data.getRange(position, totalFileHeaderSize); + this.fileHeaders.add(new CentralDirectoryFileHeader(buffer)); + + // Move the position pointer forward. + position += totalFileHeaderSize; + + // Break out of the loop if the next 4 bytes do not match the right file header signature. + if (this._data.length >= position + signatureSize && !listsAreEqual(this._data.getRange(position, signatureSize), signatureCodes)) { + break; + } + } else { + break; + } + } + + // TODO: Process the possible digital signature. + } +} \ No newline at end of file diff --git a/lib/CentralDirectoryFileHeader.dart b/lib/CentralDirectoryFileHeader.dart new file mode 100644 index 0000000..6d46183 --- /dev/null +++ b/lib/CentralDirectoryFileHeader.dart @@ -0,0 +1,95 @@ +/*! + * DartZip + * + * Copyright (C) 2012, Kai Sellgren + * Licensed under the MIT License. + * http://www.opensource.org/licenses/mit-license.php + */ + +#library('CentralDirectoryFileHeader'); + +#import('Zip.dart'); +#import('Util.dart'); +#import('dart:utf'); + +/** + * Creates a new instance of the Central Directory File Header. + */ +class CentralDirectoryFileHeader { + List _data; + + var signature = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE; + var versionMadeBy; + var versionNeededToExtract; + var generalPurposeBitFlag; + var compressionMethod; + var lastModifiedFileTime; + var lastModifiedFileDate; + var crc32; + var compressedSize; + var uncompressedSize; + var filenameLength; + var extraFieldLength; + var fileCommentLength; + var diskNumberStart; + var internalFileAttributes; + var externalFileAttributes; + var localHeaderOffset; + var filename; + var extraField; + var fileComment; + + CentralDirectoryFileHeader(List data) { + this._data = data; + this._process(); + } + +/** +* Reads the data and sets the information to class members. +*/ + void _process() { + // File header: + // + // central file header signature 4 bytes (0x02014b50) + // version made by 2 bytes + // version needed to extract 2 bytes + // general purpose bit flag 2 bytes + // compression method 2 bytes + // last mod file time 2 bytes + // last mod file date 2 bytes + // crc-32 4 bytes + // compressed size 4 bytes + // uncompressed size 4 bytes + // file name length 2 bytes + // extra field length 2 bytes + // file comment length 2 bytes + // disk number start 2 bytes + // internal file attributes 2 bytes + // external file attributes 4 bytes + // relative offset of local header 4 bytes + // + // file name (variable size) + // extra field (variable size) + // file comment (variable size) + + this.versionMadeBy = this._data.getRange(4, 2); + this.versionNeededToExtract = this._data.getRange(6, 2); + this.generalPurposeBitFlag = this._data.getRange(8, 2); + this.compressionMethod = this._data.getRange(10, 2); + this.lastModifiedFileTime = this._data.getRange(12, 2); + this.lastModifiedFileDate = this._data.getRange(14, 2); + this.crc32 = this._data.getRange(16, 4); + this.compressedSize = bytesToValue(this._data.getRange(20, 4)); + this.uncompressedSize = bytesToValue(this._data.getRange(24, 4)); + this.filenameLength = bytesToValue(this._data.getRange(28, 2)); + this.extraFieldLength = bytesToValue(this._data.getRange(30, 2)); + this.fileCommentLength = bytesToValue(this._data.getRange(32, 2)); + this.diskNumberStart = bytesToValue(this._data.getRange(34, 2)); + this.internalFileAttributes = bytesToValue(this._data.getRange(36, 2)); + this.externalFileAttributes = bytesToValue(this._data.getRange(38, 4)); + this.localHeaderOffset = bytesToValue(this._data.getRange(42, 4)); + this.filename = new String.fromCharCodes(this._data.getRange(46, this.filenameLength)); + this.extraField = this._data.getRange(46 + this.filenameLength, this.extraFieldLength); + this.fileComment = this._data.getRange(46 + this.filenameLength + this.extraFieldLength, this.fileCommentLength); + } +} \ No newline at end of file diff --git a/lib/EndOfCentralDirectoryRecord.dart b/lib/EndOfCentralDirectoryRecord.dart new file mode 100644 index 0000000..8ba2b42 --- /dev/null +++ b/lib/EndOfCentralDirectoryRecord.dart @@ -0,0 +1,59 @@ +/*! + * DartZip + * + * Copyright (C) 2012, Kai Sellgren + * Licensed under the MIT License. + * 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. + */ +class EndOfCentralDirectoryRecord { + List _data; + + var signature = Zip.END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE; + var totalCentralDirectoryEntries; + var centralDirectorySize; + var centralDirectoryOffset; + var zipFileCommentLength; + var zipFileComment; + + EndOfCentralDirectoryRecord(List data) { + this._data = data; + this._process(); + } + + /** + * Reads the data and sets the information to class members. + */ + void _process() { + // I. End of central directory record: + // + // end of central dir signature 4 bytes (0x06054b50) + // number of this disk 2 bytes + // number of the disk with the + // start of the central directory 2 bytes + // total number of entries in the + // central directory on this disk 2 bytes + // total number of entries in + // the central directory 2 bytes + // size of the central directory 4 bytes + // offset of start of central + // directory with respect to + // the starting disk number 4 bytes + // .ZIP file comment length 2 bytes + // .ZIP file comment (variable size) + + this.totalCentralDirectoryEntries = bytesToValue(this._data.getRange(10, 2)); + this.centralDirectorySize = bytesToValue(this._data.getRange(12, 4)); + this.centralDirectoryOffset = bytesToValue(this._data.getRange(16, 4)); + this.zipFileCommentLength = bytesToValue(this._data.getRange(20, 2)); + this.zipFileComment = bytesToValue(this._data.getRange(22, this.zipFileCommentLength)); + } +} \ No newline at end of file diff --git a/lib/Util.dart b/lib/Util.dart new file mode 100644 index 0000000..7536313 --- /dev/null +++ b/lib/Util.dart @@ -0,0 +1,26 @@ +#library('Util'); + +/** + * Converts the byte sequence to a numeric representation. + */ +bytesToValue(List bytes) { + var value = 0; + + for (var i = 0, length = bytes.length; i < length; i++) { + value += bytes[i] * Math.pow(256, i); + } + + return value; +} + +/** +* Returns true if the two given lists are equal. +*/ +bool listsAreEqual(List one, List two) { + var i = -1; + return one.every((element) { + i++; + + return two[i] == element; + }); +} \ No newline at end of file diff --git a/lib/Zip.dart b/lib/Zip.dart new file mode 100644 index 0000000..28db0c5 --- /dev/null +++ b/lib/Zip.dart @@ -0,0 +1,99 @@ +/*! + * DartZip + * + * Copyright (C) 2012, Kai Sellgren + * Licensed under the MIT License. + * http://www.opensource.org/licenses/mit-license.php + */ + +#library('Zip'); + +#import('dart:io'); +#import('EndOfCentralDirectoryRecord.dart'); +#import('CentralDirectory.dart'); +#import('Util.dart'); + +/** + * This class represents a Zip file. + */ +class Zip { + static final LOCAL_FILE_HEADER_SIGNATURE = "\x50\x4b\x03\x04"; + static final DATA_DESCRIPTOR_SIGNATURE = "\x50\x4b\x07\x08"; + static final CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE = "\x50\x4b\x01\x02"; + static final END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE = "\x50\x4b\x05\x06"; + static final CENTRAL_DIRECTORY_DIGITAL_SIGNATURE_SIGNATURE = "\x50\x4b\x05\x05"; + + Path _filePath; + File _file; + List _data; + + EndOfCentralDirectoryRecord _endOfCentralDirectoryRecord; + CentralDirectory _centralDirectory; + List files; + + Zip(Path filePath) { + this._filePath = filePath; + } + + /** + * Open the Zip file for reading. + * + * TODO: Return a Future? + */ + Future open() { + var completer = new Completer(); + + this._file = new File.fromPath(this._filePath); + + this._file.readAsBytes().then((bytes) { + this._data = bytes; + this._process(); + + completer.complete(1); + }); + + return completer.future; + } + + /** + * Processes the Zip file contents. + */ + void _process() { + var position = this._getEndOfCentralDirectoryRecordPosition(); + if (position == false) { + throw new Exception('Could not locate the End of Central Directory Record. The archive seems to be a corrupted Zip archive.'); + } + + this._endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord(this._data.getRange(position, this._data.length - position)); + + // Create Central Directory object. + var centralDirectoryOffset = this._endOfCentralDirectoryRecord.centralDirectoryOffset; + var centralDirectorySize = this._endOfCentralDirectoryRecord.centralDirectorySize; + this._centralDirectory = new CentralDirectory(this._data.getRange(centralDirectoryOffset, centralDirectorySize)); + + // Create Local Files. + print(this._centralDirectory.fileHeaders[0].uncompressedSize); + } + + /** + * Finds the position of the End of Central Directory. + */ + int _getEndOfCentralDirectoryRecordPosition() { + // I want to shoot the smart ass who had the great idea of having an arbitrary sized comment field in this header. + var signatureSize = Zip.END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE.length; + var signatureCodes = Zip.END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE.charCodes(); + var maxScanLength = 65536; + var length = this._data.length; + var position = length - signatureSize; + + // Start looping from the end of the data sequence. + for (; position > length - maxScanLength && position > 0; position--) { + // If we found the end of central directory record signature, return the current position. + if (listsAreEqual(signatureCodes, this._data.getRange(position, signatureSize))) { + return position; + } + } + + return false; + } +} \ No newline at end of file diff --git a/test/test.dart b/test/test.dart new file mode 100644 index 0000000..0cd6d57 --- /dev/null +++ b/test/test.dart @@ -0,0 +1,10 @@ +#import('../lib/Zip.dart'); +#import('dart:io'); + +void main() { + var zip = new Zip(new Path('test.zip')); + + zip.open().then((i) { + print(zip.files); + }); +} \ No newline at end of file diff --git a/test/test.zip b/test/test.zip new file mode 100644 index 0000000..d51239e Binary files /dev/null and b/test/test.zip differ