0.1.2 release
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
# SSZipArchive Changelog
|
||||
|
||||
### Version 0.1.2
|
||||
|
||||
[Released December 26, 2011](https://github.com/samsoffes/sskeychain/tree/0.1.2)
|
||||
|
||||
* Add creation support. Thanks Johnnie Walker ([@randomsequence](https://github.com/randomsequence))!
|
||||
|
||||
### Version 0.1.1
|
||||
|
||||
[Released October 31, 2011](https://github.com/samsoffes/sskeychain/tree/0.1.1)
|
||||
|
||||
+23
-5
@@ -1,8 +1,13 @@
|
||||
# SSZipArchive
|
||||
|
||||
SSZipArchive is a simple utility class for unzipping files originally based on [ZipArchive](http://code.google.com/p/ziparchive) by aish.
|
||||
SSZipArchive is a simple utility class for unzipping files originally based on [ZipArchive](http://code.google.com/p/ziparchive) by aish. Features:
|
||||
|
||||
Currently it only supports unzipping. In the future, creating zip files will be supported.
|
||||
* Unzipping zip files
|
||||
* Unzipping password protected zip files
|
||||
* Creating zip files
|
||||
* Appending to zip files
|
||||
* Zipping files
|
||||
* Zipping NSData with a filename
|
||||
|
||||
## Adding to your project
|
||||
|
||||
@@ -12,11 +17,24 @@ Currently it only supports unzipping. In the future, creating zip files will be
|
||||
## Usage
|
||||
|
||||
``` objective-c
|
||||
NSString *path = @"path_to_your_zip_file";
|
||||
NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped";
|
||||
[SSZipArchive unzipFileAtPath:path toDestination:destination];
|
||||
// Unzipping
|
||||
NSString *zipPath = @"path_to_your_zip_file";
|
||||
NSString *destinationPath = @"path_to_the_folder_where_you_want_it_unzipped";
|
||||
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
|
||||
|
||||
// Zipping
|
||||
NSString *zippedPath = @"path_where_you_want_the_file_created";
|
||||
NSArray *inputPaths = [NSArray arrayWithObjects:
|
||||
[[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],
|
||||
[[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"]
|
||||
nil];
|
||||
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
SSZipArchive is licensed under the [MIT license](https://github.com/samsoffes/ssziparchive/raw/master/LICENSE). A slightly modified version of [Minizip](http://www.winimage.com/zLibDll/minizip.html) 1.1 is also included and is licensed under the [Zlib license](http://www.zlib.net/zlib_license.html).
|
||||
|
||||
## Thanks
|
||||
|
||||
Thanks [aish](http://code.google.com/p/ziparchive) for creating [ZipArchive](http://code.google.com/p/ziparchive), Johnnie Walker ([@randomsequence](https://github.com/randomsequence)) for implementing creation support, and John Engelhart ([@johnezang](https://github.com/johnezang)) for his help along the way.
|
||||
|
||||
+1
-5
@@ -9,11 +9,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "minizip/zip.h"
|
||||
|
||||
@interface SSZipArchive : NSObject {
|
||||
@private
|
||||
NSString *_path;
|
||||
zipFile _zip;
|
||||
}
|
||||
@interface SSZipArchive : NSObject
|
||||
|
||||
// Unzip
|
||||
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;
|
||||
|
||||
+81
-103
@@ -12,12 +12,19 @@
|
||||
#import "zlib.h"
|
||||
#import "zconf.h"
|
||||
|
||||
#define CHUNK 16384
|
||||
|
||||
@interface SSZipArchive ()
|
||||
+ (NSDate *)_dateFor1980;
|
||||
@end
|
||||
|
||||
|
||||
@implementation SSZipArchive
|
||||
@implementation SSZipArchive {
|
||||
NSString *_path;
|
||||
NSString *_filename;
|
||||
zipFile _zip;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Unzipping
|
||||
|
||||
@@ -148,129 +155,100 @@
|
||||
return success;
|
||||
}
|
||||
|
||||
#pragma mark - unzip
|
||||
|
||||
#define CHUNK 16384
|
||||
#pragma mark - Zipping
|
||||
|
||||
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths {
|
||||
|
||||
BOOL success = NO;
|
||||
|
||||
SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path];
|
||||
if ([zipArchive open]) {
|
||||
|
||||
for (NSString *path in paths) {
|
||||
[zipArchive writeFile:path];
|
||||
}
|
||||
|
||||
success = [zipArchive close];
|
||||
}
|
||||
[zipArchive release];
|
||||
|
||||
return success;
|
||||
BOOL success = NO;
|
||||
SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path];
|
||||
if ([zipArchive open]) {
|
||||
for (NSString *path in paths) {
|
||||
[zipArchive writeFile:path];
|
||||
}
|
||||
success = [zipArchive close];
|
||||
}
|
||||
[zipArchive release];
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
- (id)initWithPath:(NSString *)path
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_path = [path copy];
|
||||
// Initialization code here.
|
||||
}
|
||||
|
||||
return self;
|
||||
|
||||
- (id)initWithPath:(NSString *)path {
|
||||
if ((self = [super init])) {
|
||||
_path = [path copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (void)dealloc {
|
||||
[_path release];
|
||||
[super dealloc];
|
||||
[_path release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (BOOL)open {
|
||||
|
||||
NSAssert((_zip == NULL), @"Attempting open an archive which is already open");
|
||||
|
||||
_zip = zipOpen([_path UTF8String], APPEND_STATUS_CREATE);
|
||||
|
||||
return (NULL != _zip);
|
||||
|
||||
- (BOOL)open {
|
||||
NSAssert((_zip == NULL), @"Attempting open an archive which is already open");
|
||||
_zip = zipOpen([_path UTF8String], APPEND_STATUS_CREATE);
|
||||
return (NULL != _zip);
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)writeFile:(NSString *)path {
|
||||
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
|
||||
|
||||
FILE *input = fopen([path UTF8String], "r");
|
||||
if (NULL == input) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
zipOpenNewFileInZip(_zip,
|
||||
[[path lastPathComponent] UTF8String],
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
Z_DEFLATED,
|
||||
Z_DEFAULT_COMPRESSION);
|
||||
|
||||
void *buffer = malloc(CHUNK);
|
||||
unsigned int len = 0;
|
||||
|
||||
while (!feof(input)) {
|
||||
len = (unsigned int) fread(buffer, 1, CHUNK, input);
|
||||
zipWriteInFileInZip(_zip, buffer, len);
|
||||
}
|
||||
|
||||
zipCloseFileInZip(_zip);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return YES;
|
||||
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
|
||||
|
||||
FILE *input = fopen([path UTF8String], "r");
|
||||
if (NULL == input) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
zipOpenNewFileInZip(_zip, [[path lastPathComponent] UTF8String], NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED,
|
||||
Z_DEFAULT_COMPRESSION);
|
||||
|
||||
void *buffer = malloc(CHUNK);
|
||||
unsigned int len = 0;
|
||||
while (!feof(input)) {
|
||||
len = (unsigned int) fread(buffer, 1, CHUNK, input);
|
||||
zipWriteInFileInZip(_zip, buffer, len);
|
||||
}
|
||||
|
||||
zipCloseFileInZip(_zip);
|
||||
free(buffer);
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename {
|
||||
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
|
||||
NSAssert((data != NULL), @"Nil data");
|
||||
|
||||
zipOpenNewFileInZip(_zip,
|
||||
[filename UTF8String],
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
Z_DEFLATED,
|
||||
Z_DEFAULT_COMPRESSION);
|
||||
|
||||
void *buffer = malloc(CHUNK);
|
||||
unsigned int offset = 0;
|
||||
NSUInteger length = [data length];
|
||||
unsigned int chunk_length;
|
||||
|
||||
while (offset < length) {
|
||||
chunk_length = MIN(CHUNK, ((unsigned int) length)-offset);
|
||||
[data getBytes:&buffer range:NSMakeRange(offset, chunk_length)];
|
||||
zipWriteInFileInZip(_zip, buffer, chunk_length);
|
||||
offset += chunk_length;
|
||||
}
|
||||
|
||||
zipCloseFileInZip(_zip);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return YES;
|
||||
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
|
||||
NSAssert((data != NULL), @"Nil data");
|
||||
|
||||
zipOpenNewFileInZip(_zip, [filename UTF8String], NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
|
||||
|
||||
void *buffer = malloc(CHUNK);
|
||||
unsigned int offset = 0;
|
||||
NSUInteger length = [data length];
|
||||
unsigned int chunk_length = 0;
|
||||
|
||||
while (offset < length) {
|
||||
chunk_length = MIN(CHUNK, ((unsigned int) length)-offset);
|
||||
[data getBytes:&buffer range:NSMakeRange(offset, chunk_length)];
|
||||
zipWriteInFileInZip(_zip, buffer, chunk_length);
|
||||
offset += chunk_length;
|
||||
}
|
||||
|
||||
zipCloseFileInZip(_zip);
|
||||
free(buffer);
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)close {
|
||||
NSAssert((_zip != NULL), @"Attempting to close an archive which was never opened");
|
||||
|
||||
zipClose(_zip, NULL);
|
||||
|
||||
return YES;
|
||||
NSAssert((_zip != NULL), @"Attempting to close an archive which was never opened");
|
||||
zipClose(_zip, NULL);
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
+ (NSDate *)_dateFor1980 {
|
||||
|
||||
@@ -10,4 +10,7 @@
|
||||
|
||||
@interface SSZipArchiveTests : SenTestCase
|
||||
|
||||
- (void)testZipping;
|
||||
- (void)testUnzipping;
|
||||
|
||||
@end
|
||||
|
||||
+16
-28
@@ -11,28 +11,27 @@
|
||||
|
||||
@interface SSZipArchiveTests ()
|
||||
- (NSString *)_cachesPath;
|
||||
- (NSString *)_createOutputPath;
|
||||
@end
|
||||
|
||||
@implementation SSZipArchiveTests
|
||||
|
||||
- (void)testBasicZipping {
|
||||
NSString *outputPath = [self _createOutputPath];
|
||||
|
||||
NSArray *inputPaths = [NSArray arrayWithObjects:
|
||||
[outputPath stringByAppendingPathComponent:@"Readme.markdown"],
|
||||
[outputPath stringByAppendingPathComponent:@"LICENSE"],
|
||||
nil];
|
||||
NSString *archivePath = [outputPath stringByAppendingPathComponent:@"CreatedArchive.zip"];
|
||||
[SSZipArchive createZipFileAtPath:archivePath withFilesAtPaths:inputPaths];
|
||||
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
STAssertTrue([fileManager fileExistsAtPath:archivePath], @"Archive created");
|
||||
- (void)testZipping {
|
||||
NSString *outputPath = [self _cachesPath];
|
||||
NSArray *inputPaths = [NSArray arrayWithObjects:
|
||||
[outputPath stringByAppendingPathComponent:@"Readme.markdown"],
|
||||
[outputPath stringByAppendingPathComponent:@"LICENSE"],
|
||||
nil];
|
||||
NSString *archivePath = [outputPath stringByAppendingPathComponent:@"CreatedArchive.zip"];
|
||||
[SSZipArchive createZipFileAtPath:archivePath withFilesAtPaths:inputPaths];
|
||||
|
||||
// TODO: Make sure the files are actually unzipped. They are, but the test should be better.
|
||||
STAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:archivePath], @"Archive created");
|
||||
}
|
||||
|
||||
- (void)testBasicUnzipping {
|
||||
|
||||
- (void)testUnzipping {
|
||||
NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestArchive" ofType:@"zip"];
|
||||
NSString *outputPath = [self _createOutputPath];
|
||||
NSString *outputPath = [self _cachesPath];
|
||||
|
||||
[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath];
|
||||
|
||||
@@ -43,6 +42,8 @@
|
||||
testPath = [outputPath stringByAppendingPathComponent:@"LICENSE"];
|
||||
STAssertTrue([fileManager fileExistsAtPath:testPath], @"LICENSE unzipped");
|
||||
|
||||
// Commented out to avoid checking in several gig file into the repository. Simply add a file named
|
||||
// `LargeArchive.zip` to the project and uncomment out these lines to test.
|
||||
// zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"LargeArchive" ofType:@"zip"];
|
||||
// outputPath = [[self _cachesPath] stringByAppendingPathComponent:@"large"];
|
||||
// [SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath];
|
||||
@@ -51,19 +52,6 @@
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
- (NSString *)_createOutputPath {
|
||||
NSString *outputPath = [[self _cachesPath] stringByAppendingPathComponent:@"basic"];
|
||||
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
if (![fileManager fileExistsAtPath:outputPath]) {
|
||||
[fileManager createDirectoryAtPath:outputPath withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
}
|
||||
|
||||
// NSLog(@"outputPath: %@", outputPath);
|
||||
|
||||
return outputPath;
|
||||
}
|
||||
|
||||
- (NSString *)_cachesPath {
|
||||
return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
|
||||
stringByAppendingPathComponent:@"com.samsoffes.ssziparchive.tests"];
|
||||
|
||||
Reference in New Issue
Block a user