Fixed markdown on GETTING_STARTED; added LICENSE
This commit is contained in:
+49
-49
@@ -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
|
||||
------------------
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
|
||||
Copyright (c) 2009-2012, Flying Dolphin Studio
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Flying Dolphin Studio nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
||||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
Reference in New Issue
Block a user