Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisellgren committed Jul 31, 2012
0 parents commit d747652
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DartZip
==
A Zib library written in Dart.

## License
The library is licensed under MIT.
102 changes: 102 additions & 0 deletions lib/CentralDirectory.dart
Original file line number Diff line number Diff line change
@@ -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<int> _data;

static final FILE_HEADER_STATIC_SIZE = 46; // The static size of the file header.

var signature = Zip.CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE;
List<CentralDirectoryFileHeader> fileHeaders;
var digitalSignature;

CentralDirectory(List<int> 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.
}
}
95 changes: 95 additions & 0 deletions lib/CentralDirectoryFileHeader.dart
Original file line number Diff line number Diff line change
@@ -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<int> _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<int> 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);
}
}
59 changes: 59 additions & 0 deletions lib/EndOfCentralDirectoryRecord.dart
Original file line number Diff line number Diff line change
@@ -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<int> _data;

var signature = Zip.END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE;
var totalCentralDirectoryEntries;
var centralDirectorySize;
var centralDirectoryOffset;
var zipFileCommentLength;
var zipFileComment;

EndOfCentralDirectoryRecord(List<int> 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));
}
}
26 changes: 26 additions & 0 deletions lib/Util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#library('Util');

/**
* Converts the byte sequence to a numeric representation.
*/
bytesToValue(List<int> 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;
});
}
Loading

0 comments on commit d747652

Please sign in to comment.