Fixed markdown on GETTING_STARTED; added LICENSE

This commit is contained in:
Gianluca Bertani
2012-11-28 23:47:27 +01:00
parent 9e6a610c29
commit c26ee21bda
2 changed files with 79 additions and 49 deletions
+49 -49
View File
@@ -27,8 +27,8 @@ ZipFile. It can be created with the common Objective-C procedure of an
alloc followed by an init, specifying in the latter if the zip file is
being created, appended or unzipped:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeCreate];
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeCreate];
Creating and appending are both write-only modalities, while unzipping
is a read-only modality. You can not request reading operations on a
@@ -44,11 +44,11 @@ with a password. Both methods return an instance of a ZipWriteStream
class, which will be used solely for the scope of writing the content of
the file, and then must be closed:
ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt"
compressionLevel:ZipCompressionLevelBest];
ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt"
compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
[stream writeData:abcData];
[stream finishedWriting];
Reading a file from a zip file
------------------------------
@@ -59,16 +59,16 @@ step-forwarding or by locating the file by name. Once you are on the
correct file, you can obtain an instance of a ZipReadStream that will
let you read the content (and then must be closed):
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeUnzip];
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
[read finishedReading];
Note that the NSMutableData instance that acts as the read buffer must
have been set with a length greater than 0: the readDataWithBuffer API
@@ -83,23 +83,23 @@ contained in zip by filling an NSArray with instances of FileInZipInfo
class. You can then use its name property to locate the file inside the
zip and expand it:
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeUnzip];
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size,
info.level);
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size,
info.level);
// Locate the file in the zip
[unzipFile locateFileInZip:info.name];
// Locate the file in the zip
[unzipFile locateFileInZip:info.name];
// Expand the file in memory
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
}
// Expand the file in memory
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
}
Note that the FileInZipInfo class provide two sizes:
- length is the original (uncompressed) file size, while
@@ -111,7 +111,7 @@ Closing the zip file
Remember, when you are done, to close your ZipFile instance to avoid
file corruption problems:
[zipFile close];
[zipFile close];
Notes
=====
@@ -132,35 +132,35 @@ Memory management
If you need to extract huge files that cannot be contained in memory,
you can do so using a read-then-write buffered loop like this:
NSFileHandle *file= [NSFileHandle fileHandleForWritingAtPath:filePath];
NSMutableData *buffer= [[NSMutableData alloc]
initWithLength:BUFFER_SIZE];
NSFileHandle *file= [NSFileHandle fileHandleForWritingAtPath:filePath];
NSMutableData *buffer= [[NSMutableData alloc]
initWithLength:BUFFER_SIZE];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
// Read-then-write buffered loop
do {
// Read-then-write buffered loop
do {
// Reset buffer length
[buffer setLength:BUFFER_SIZE];
// Reset buffer length
[buffer setLength:BUFFER_SIZE];
// Expand next chunk of bytes
int bytesRead= [read readDataWithBuffer:buffer];
if (bytesRead > 0) {
// Expand next chunk of bytes
int bytesRead= [read readDataWithBuffer:buffer];
if (bytesRead > 0) {
// Write what we have read
[buffer setLength:bytesRead];
[file writeData:buffer];
// Write what we have read
[buffer setLength:bytesRead];
[file writeData:buffer];
} else
break;
} else
break;
} while (YES);
} while (YES);
// Clean up
[file closeFile];
[read finishedReading];
[buffer release];
// Clean up
[file closeFile];
[read finishedReading];
[buffer release];
Exception handling
------------------