Skip to content

Commit

Permalink
Aesthetic changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisellgren committed Sep 23, 2012
1 parent 6c1cd30 commit 0e1a2a7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
6 changes: 4 additions & 2 deletions lib/Util.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#library('Util');
library Util;

import 'dart:math';

/**
* Converts the byte sequence to a numeric representation.
Expand All @@ -7,7 +9,7 @@ bytesToValue(List<int> 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;
Expand Down
16 changes: 9 additions & 7 deletions lib/Zip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,6 +30,7 @@ class Zip {
EndOfCentralDirectoryRecord _endOfCentralDirectoryRecord;
CentralDirectory _centralDirectory;
List files;
bool _initialized = false;

Zip(Path this._filePath);

Expand All @@ -40,6 +41,7 @@ class Zip {
*/
Future<Exception> open() {
var completer = new Completer();
_initialized = true;

this._file = new File.fromPath(this._filePath);

Expand All @@ -48,7 +50,7 @@ class Zip {

try {
this._process();
} catch (Exception e) {
} on Exception catch (e) {
completer.completeException(e);
}

Expand Down Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions lib/CentralDirectory.dart → lib/central_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions lib/LocalFileHeader.dart → lib/local_file_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> _chunk;
Expand Down
8 changes: 5 additions & 3 deletions test/test.dart
Original file line number Diff line number Diff line change
@@ -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/"));

Expand Down

0 comments on commit 0e1a2a7

Please sign in to comment.