From 48a53904d8132c7045ac9dcb915cdc41754a213e Mon Sep 17 00:00:00 2001 From: Gianluca Bertani Date: Thu, 27 Aug 2015 17:18:43 +0200 Subject: [PATCH] Some code clean up --- Classes/Objective_ZipViewController.m | 6 +- Objective-Zip.xcodeproj/project.pbxproj | 14 +++ Objective-Zip/OZFileInZipInfo+Internals.h | 46 ++++++++ Objective-Zip/OZFileInZipInfo.h | 20 +--- Objective-Zip/OZFileInZipInfo.m | 34 +++++- Objective-Zip/OZZipCompressionLevel.h | 46 ++++++++ Objective-Zip/OZZipException+Internals.h | 46 ++++++++ Objective-Zip/OZZipException.h | 14 +-- Objective-Zip/OZZipException.m | 31 ++++- Objective-Zip/OZZipFile.h | 59 +++++----- Objective-Zip/OZZipFile.m | 130 ++++++++++++++------- Objective-Zip/OZZipFileMode.h | 45 +++++++ Objective-Zip/OZZipReadStream+Internals.h | 48 ++++++++ Objective-Zip/OZZipReadStream.h | 16 +-- Objective-Zip/OZZipReadStream.m | 30 ++++- Objective-Zip/OZZipWriteStream+Internals.h | 48 ++++++++ Objective-Zip/OZZipWriteStream.h | 16 +-- Objective-Zip/OZZipWriteStream.m | 30 ++++- Objective-Zip/Objective-Zip.h | 40 +++++++ 19 files changed, 595 insertions(+), 124 deletions(-) create mode 100644 Objective-Zip/OZFileInZipInfo+Internals.h create mode 100644 Objective-Zip/OZZipCompressionLevel.h create mode 100644 Objective-Zip/OZZipException+Internals.h create mode 100644 Objective-Zip/OZZipFileMode.h create mode 100644 Objective-Zip/OZZipReadStream+Internals.h create mode 100644 Objective-Zip/OZZipWriteStream+Internals.h create mode 100644 Objective-Zip/Objective-Zip.h diff --git a/Classes/Objective_ZipViewController.m b/Classes/Objective_ZipViewController.m index a0e487e..ddee2d1 100644 --- a/Classes/Objective_ZipViewController.m +++ b/Classes/Objective_ZipViewController.m @@ -32,11 +32,7 @@ // #import "Objective_ZipViewController.h" -#import "../Objective-Zip/OZZipFile.h" -#import "../Objective-Zip/OZZipException.h" -#import "../Objective-Zip/OZFileInZipInfo.h" -#import "../Objective-Zip/OZZipWriteStream.h" -#import "../Objective-Zip/OZZipReadStream.h" +#import "Objective-Zip.h" #define HUGE_TEST_BLOCK_LENGTH (50000) #define HUGE_TEST_NUMBER_OF_BLOCKS (100000) diff --git a/Objective-Zip.xcodeproj/project.pbxproj b/Objective-Zip.xcodeproj/project.pbxproj index 9990da1..c265646 100755 --- a/Objective-Zip.xcodeproj/project.pbxproj +++ b/Objective-Zip.xcodeproj/project.pbxproj @@ -82,6 +82,13 @@ 8C8F2D9210EBCE0300F75833 /* ObjectiveZipIcon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ObjectiveZipIcon.png; sourceTree = ""; }; 8CBE431610E95FA300AC9ED3 /* OZZipReadStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OZZipReadStream.h; path = "Objective-Zip/OZZipReadStream.h"; sourceTree = ""; }; 8CBE431710E95FA300AC9ED3 /* OZZipReadStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OZZipReadStream.m; path = "Objective-Zip/OZZipReadStream.m"; sourceTree = ""; }; + 8CC6A7931B8F5B360062D97E /* Objective-Zip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "Objective-Zip.h"; path = "Objective-Zip/Objective-Zip.h"; sourceTree = ""; }; + 8CC6A7941B8F5C370062D97E /* OZZipFileMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OZZipFileMode.h; path = "Objective-Zip/OZZipFileMode.h"; sourceTree = ""; }; + 8CC6A7951B8F5C810062D97E /* OZZipCompressionLevel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OZZipCompressionLevel.h; path = "Objective-Zip/OZZipCompressionLevel.h"; sourceTree = ""; }; + 8CC6A7961B8F5F570062D97E /* OZZipException+Internals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "OZZipException+Internals.h"; path = "Objective-Zip/OZZipException+Internals.h"; sourceTree = ""; }; + 8CC6A7971B8F5F780062D97E /* OZZipWriteStream+Internals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "OZZipWriteStream+Internals.h"; path = "Objective-Zip/OZZipWriteStream+Internals.h"; sourceTree = ""; }; + 8CC6A7981B8F5F950062D97E /* OZFileInZipInfo+Internals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "OZFileInZipInfo+Internals.h"; path = "Objective-Zip/OZFileInZipInfo+Internals.h"; sourceTree = ""; }; + 8CC6A7991B8F5FB40062D97E /* OZZipReadStream+Internals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "OZZipReadStream+Internals.h"; path = "Objective-Zip/OZZipReadStream+Internals.h"; sourceTree = ""; }; 8CD8B3E917766067005212EC /* adler32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = adler32.c; path = ZLib/adler32.c; sourceTree = ""; }; 8CD8B3EA17766067005212EC /* compress.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = compress.c; path = ZLib/compress.c; sourceTree = ""; }; 8CD8B3EB17766067005212EC /* crc32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = crc32.c; path = ZLib/crc32.c; sourceTree = ""; }; @@ -215,15 +222,22 @@ 8C6D353B10E56BA400B63EFA /* Objective-Zip */ = { isa = PBXGroup; children = ( + 8CC6A7931B8F5B360062D97E /* Objective-Zip.h */, 8C6D353E10E56BD300B63EFA /* OZZipFile.h */, 8C6D353F10E56BD300B63EFA /* OZZipFile.m */, + 8CC6A7941B8F5C370062D97E /* OZZipFileMode.h */, + 8CC6A7951B8F5C810062D97E /* OZZipCompressionLevel.h */, 8C6D356910E56F4D00B63EFA /* OZZipException.h */, + 8CC6A7961B8F5F570062D97E /* OZZipException+Internals.h */, 8C6D356A10E56F4D00B63EFA /* OZZipException.m */, 8C6D357410E5797600B63EFA /* OZZipWriteStream.h */, + 8CC6A7971B8F5F780062D97E /* OZZipWriteStream+Internals.h */, 8C6D357510E5797600B63EFA /* OZZipWriteStream.m */, 8C83F49310E7CBCB002FB3CB /* OZFileInZipInfo.h */, + 8CC6A7981B8F5F950062D97E /* OZFileInZipInfo+Internals.h */, 8C83F49410E7CBCB002FB3CB /* OZFileInZipInfo.m */, 8CBE431610E95FA300AC9ED3 /* OZZipReadStream.h */, + 8CC6A7991B8F5FB40062D97E /* OZZipReadStream+Internals.h */, 8CBE431710E95FA300AC9ED3 /* OZZipReadStream.m */, ); name = "Objective-Zip"; diff --git a/Objective-Zip/OZFileInZipInfo+Internals.h b/Objective-Zip/OZFileInZipInfo+Internals.h new file mode 100644 index 0000000..41c21a7 --- /dev/null +++ b/Objective-Zip/OZFileInZipInfo+Internals.h @@ -0,0 +1,46 @@ +// +// OZFileInZipInfo+Internals.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#import "OZFileInZipInfo.h" + + +@interface OZFileInZipInfo (Internals) + + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithName:(NSString *)name length:(NSUInteger)length level:(OZZipCompressionLevel)level crypted:(BOOL)crypted size:(NSUInteger)size date:(NSDate *)date crc32:(NSUInteger)crc32; + + +@end diff --git a/Objective-Zip/OZFileInZipInfo.h b/Objective-Zip/OZFileInZipInfo.h index d74571d..86f4c42 100644 --- a/Objective-Zip/OZFileInZipInfo.h +++ b/Objective-Zip/OZFileInZipInfo.h @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 27/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -33,22 +33,13 @@ #import -#import "OZZipFile.h" +#import "OZZipCompressionLevel.h" -@interface OZFileInZipInfo : NSObject { - -@private - NSUInteger _length; - OZZipCompressionLevel _level; - BOOL _crypted; - NSUInteger _size; - NSDate *_date; - NSUInteger _crc32; - NSString *_name; -} +@interface OZFileInZipInfo : NSObject -- (id) initWithName:(NSString *)name length:(NSUInteger)length level:(OZZipCompressionLevel)level crypted:(BOOL)crypted size:(NSUInteger)size date:(NSDate *)date crc32:(NSUInteger)crc32; +#pragma mark - +#pragma mark Properties @property (nonatomic, readonly) NSString *name; @property (nonatomic, readonly) NSUInteger length; @@ -58,4 +49,5 @@ @property (nonatomic, readonly) NSDate *date; @property (nonatomic, readonly) NSUInteger crc32; + @end diff --git a/Objective-Zip/OZFileInZipInfo.m b/Objective-Zip/OZFileInZipInfo.m index 9953666..2c3fa2f 100644 --- a/Objective-Zip/OZFileInZipInfo.m +++ b/Objective-Zip/OZFileInZipInfo.m @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 27/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -34,9 +34,35 @@ #import "OZFileInZipInfo.h" +#pragma mark - +#pragma mark OZFileInZipInfo extension + +@interface OZFileInZipInfo () { + +@private + NSUInteger _length; + OZZipCompressionLevel _level; + BOOL _crypted; + NSUInteger _size; + NSDate *_date; + NSUInteger _crc32; + NSString *_name; +} + + +@end + + +#pragma mark - +#pragma mark OZFileInZipInfo implementation + @implementation OZFileInZipInfo -- (id) initWithName:(NSString *)name length:(NSUInteger)length level:(OZZipCompressionLevel)level crypted:(BOOL)crypted size:(NSUInteger)size date:(NSDate *)date crc32:(NSUInteger)crc32 { + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithName:(NSString *)name length:(NSUInteger)length level:(OZZipCompressionLevel)level crypted:(BOOL)crypted size:(NSUInteger)size date:(NSDate *)date crc32:(NSUInteger)crc32 { if (self= [super init]) { _name= name; _length= length; @@ -51,6 +77,9 @@ } +#pragma mark - +#pragma mark Properties + @synthesize name= _name; @synthesize length= _length; @synthesize level= _level; @@ -59,4 +88,5 @@ @synthesize date= _date; @synthesize crc32= _crc32; + @end diff --git a/Objective-Zip/OZZipCompressionLevel.h b/Objective-Zip/OZZipCompressionLevel.h new file mode 100644 index 0000000..b4d3364 --- /dev/null +++ b/Objective-Zip/OZZipCompressionLevel.h @@ -0,0 +1,46 @@ +// +// OZZipCompressionLevel.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#ifndef Objective_Zip_OZZipCompressionLevel_h +#define Objective_Zip_OZZipCompressionLevel_h + + +typedef enum { + OZZipCompressionLevelDefault= -1, + OZZipCompressionLevelNone= 0, + OZZipCompressionLevelFastest= 1, + OZZipCompressionLevelBest= 9 +} OZZipCompressionLevel; + + +#endif diff --git a/Objective-Zip/OZZipException+Internals.h b/Objective-Zip/OZZipException+Internals.h new file mode 100644 index 0000000..ad55c0f --- /dev/null +++ b/Objective-Zip/OZZipException+Internals.h @@ -0,0 +1,46 @@ +// +// OZZipException+Internals.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#import "OZZipException.h" + +@interface OZZipException (Internals) + + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithReason:(NSString *)reason; +- (instancetype) initWithError:(NSInteger)error reason:(NSString *)reason; + + +@end diff --git a/Objective-Zip/OZZipException.h b/Objective-Zip/OZZipException.h index 9c0accb..207d7c5 100644 --- a/Objective-Zip/OZZipException.h +++ b/Objective-Zip/OZZipException.h @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -34,15 +34,13 @@ #import -@interface OZZipException : NSException { - -@private - NSInteger _error; -} +@interface OZZipException : NSException -- (id) initWithReason:(NSString *)reason; -- (id) initWithError:(NSInteger)error reason:(NSString *)reason; + +#pragma mark - +#pragma mark Properties @property (nonatomic, readonly) NSInteger error; + @end diff --git a/Objective-Zip/OZZipException.m b/Objective-Zip/OZZipException.m index 7134dce..335ffae 100644 --- a/Objective-Zip/OZZipException.m +++ b/Objective-Zip/OZZipException.m @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -34,9 +34,29 @@ #import "OZZipException.h" +#pragma mark - +#pragma mark OZZipException extension + +@interface OZZipException () { + +@private + NSInteger _error; +} + + +@end + + +#pragma mark - +#pragma mark OZZipException implementation + @implementation OZZipException -- (id) initWithReason:(NSString *)reason { + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithReason:(NSString *)reason { if (self= [super initWithName:@"OZZipException" reason:reason userInfo:nil]) { _error= 0; } @@ -44,7 +64,7 @@ return self; } -- (id) initWithError:(NSInteger)error reason:(NSString *)reason { +- (instancetype) initWithError:(NSInteger)error reason:(NSString *)reason { if (self= [super initWithName:@"OZZipException" reason:reason userInfo:nil]) { _error= error; } @@ -52,6 +72,11 @@ return self; } + +#pragma mark - +#pragma mark Properties + @synthesize error= _error; + @end diff --git a/Objective-Zip/OZZipFile.h b/Objective-Zip/OZZipFile.h index bdcafcd..a48579c 100644 --- a/Objective-Zip/OZZipFile.h +++ b/Objective-Zip/OZZipFile.h @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -33,55 +33,60 @@ #import -#include "zip.h" -#include "unzip.h" +#import "OZZipFileMode.h" +#import "OZZipCompressionLevel.h" -typedef enum { - OZZipFileModeUnzip, - OZZipFileModeCreate, - OZZipFileModeAppend -} OZZipFileMode; - -typedef enum { - OZZipCompressionLevelDefault= -1, - OZZipCompressionLevelNone= 0, - OZZipCompressionLevelFastest= 1, - OZZipCompressionLevelBest= 9 -} OZZipCompressionLevel; - @class OZZipReadStream; @class OZZipWriteStream; @class OZFileInZipInfo; -@interface OZZipFile : NSObject { - NSString *_fileName; - OZZipFileMode _mode; +@interface OZZipFile : NSObject -@private - zipFile _zipFile; - unzFile _unzFile; -} -- (id) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode; +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode; + + +#pragma mark - +#pragma mark File writing - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip compressionLevel:(OZZipCompressionLevel)compressionLevel; - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel; - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel password:(NSString *)password crc32:(NSUInteger)crc32; -- (NSString*) fileName; -- (NSUInteger) numFilesInZip; -- (NSArray *) listFileInZipInfos; + +#pragma mark - +#pragma mark File seeking and info - (void) goToFirstFileInZip; - (BOOL) goToNextFileInZip; - (BOOL) locateFileInZip:(NSString *)fileNameInZip; +- (NSArray *) listFileInZipInfos; - (OZFileInZipInfo *) getCurrentFileInZipInfo; + +#pragma mark - +#pragma mark File reading + - (OZZipReadStream *) readCurrentFileInZip; - (OZZipReadStream *) readCurrentFileInZipWithPassword:(NSString *)password; + +#pragma mark - +#pragma mark Closing + - (void) close; + +#pragma mark - +#pragma mark Properties + +@property (nonatomic, readonly) NSString *fileName; +@property (nonatomic, readonly) NSUInteger numFilesInZip; + + @end diff --git a/Objective-Zip/OZZipFile.m b/Objective-Zip/OZZipFile.m index b64cb4b..a24ef22 100644 --- a/Objective-Zip/OZZipFile.m +++ b/Objective-Zip/OZZipFile.m @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -33,17 +33,46 @@ #import "OZZipFile.h" #import "OZZipException.h" +#import "OZZipException+Internals.h" #import "OZZipReadStream.h" +#import "OZZipReadStream+Internals.h" #import "OZZipWriteStream.h" +#import "OZZipWriteStream+Internals.h" #import "OZFileInZipInfo.h" +#import "OZFileInZipInfo+Internals.h" + +#include "zip.h" +#include "unzip.h" #define FILE_IN_ZIP_MAX_NAME_LENGTH (256) +#pragma mark - +#pragma mark OZZipFile extension + +@interface OZZipFile () { + NSString *_fileName; + OZZipFileMode _mode; + +@private + zipFile _zipFile; + unzFile _unzFile; +} + + +@end + + +#pragma mark - +#pragma mark OZZipFile implementation + @implementation OZZipFile -- (id) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode { +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode { if (self= [super init]) { _fileName= fileName; _mode= mode; @@ -84,6 +113,9 @@ } +#pragma mark - +#pragma mark File writing + - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip compressionLevel:(OZZipCompressionLevel)compressionLevel { if (_mode == OZZipFileModeUnzip) { NSString *reason= [NSString stringWithFormat:@"Operation not permitted with Unzip mode"]; @@ -193,44 +225,9 @@ return [[OZZipWriteStream alloc] initWithZipFileStruct:_zipFile fileNameInZip:fileNameInZip]; } -- (NSString*) fileName { - return _fileName; -} -- (NSUInteger) numFilesInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } - - unz_global_info64 gi; - int err= unzGetGlobalInfo64(_unzFile, &gi); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting global info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } - - return gi.number_entry; -} - -- (NSArray *) listFileInZipInfos { - int num= [self numFilesInZip]; - if (num < 1) - return [[NSArray alloc] init]; - - NSMutableArray *files= [[NSMutableArray alloc] initWithCapacity:num]; - - [self goToFirstFileInZip]; - for (int i= 0; i < num; i++) { - OZFileInZipInfo *info= [self getCurrentFileInZipInfo]; - [files addObject:info]; - - if ((i +1) < num) - [self goToNextFileInZip]; - } - - return files; -} +#pragma mark - +#pragma mark File seeking and info - (void) goToFirstFileInZip { if (_mode != OZZipFileModeUnzip) { @@ -281,6 +278,25 @@ return YES; } +- (NSArray *) listFileInZipInfos { + int num= [self numFilesInZip]; + if (num < 1) + return [[NSArray alloc] init]; + + NSMutableArray *files= [[NSMutableArray alloc] initWithCapacity:num]; + + [self goToFirstFileInZip]; + for (int i= 0; i < num; i++) { + OZFileInZipInfo *info= [self getCurrentFileInZipInfo]; + [files addObject:info]; + + if ((i +1) < num) + [self goToNextFileInZip]; + } + + return files; +} + - (OZFileInZipInfo *) getCurrentFileInZipInfo { if (_mode != OZZipFileModeUnzip) { NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; @@ -331,6 +347,10 @@ return info; } + +#pragma mark - +#pragma mark File reading + - (OZZipReadStream *) readCurrentFileInZip { if (_mode != OZZipFileModeUnzip) { NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; @@ -383,6 +403,10 @@ return [[OZZipReadStream alloc] initWithUnzFileStruct:_unzFile fileNameInZip:fileNameInZip]; } + +#pragma mark - +#pragma mark Closing + - (void) close { switch (_mode) { case OZZipFileModeUnzip: { @@ -420,4 +444,32 @@ } +#pragma mark - +#pragma mark Properties + +@dynamic fileName; + +- (NSString*) fileName { + return _fileName; +} + +@dynamic numFilesInZip; + +- (NSUInteger) numFilesInZip { + if (_mode != OZZipFileModeUnzip) { + NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; + @throw [[OZZipException alloc] initWithReason:reason]; + } + + unz_global_info64 gi; + int err= unzGetGlobalInfo64(_unzFile, &gi); + if (err != UNZ_OK) { + NSString *reason= [NSString stringWithFormat:@"Error getting global info in '%@'", _fileName]; + @throw [[OZZipException alloc] initWithError:err reason:reason]; + } + + return gi.number_entry; +} + + @end diff --git a/Objective-Zip/OZZipFileMode.h b/Objective-Zip/OZZipFileMode.h new file mode 100644 index 0000000..5e209e3 --- /dev/null +++ b/Objective-Zip/OZZipFileMode.h @@ -0,0 +1,45 @@ +// +// OZZipFileMode.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#ifndef Objective_Zip_OZZipFileMode_h +#define Objective_Zip_OZZipFileMode_h + + +typedef enum { + OZZipFileModeUnzip, + OZZipFileModeCreate, + OZZipFileModeAppend +} OZZipFileMode; + + +#endif diff --git a/Objective-Zip/OZZipReadStream+Internals.h b/Objective-Zip/OZZipReadStream+Internals.h new file mode 100644 index 0000000..372c932 --- /dev/null +++ b/Objective-Zip/OZZipReadStream+Internals.h @@ -0,0 +1,48 @@ +// +// OZZipReadStream+Internals.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#import "OZZipReadStream.h" + +#include "unzip.h" + + +@interface OZZipReadStream (Internals) + + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithUnzFileStruct:(unzFile)unzFile fileNameInZip:(NSString *)fileNameInZip; + + +@end diff --git a/Objective-Zip/OZZipReadStream.h b/Objective-Zip/OZZipReadStream.h index d8e487f..831f1ee 100644 --- a/Objective-Zip/OZZipReadStream.h +++ b/Objective-Zip/OZZipReadStream.h @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 28/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -33,19 +33,15 @@ #import -#include "unzip.h" + +@interface OZZipReadStream : NSObject -@interface OZZipReadStream : NSObject { - NSString *_fileNameInZip; - -@private - unzFile _unzFile; -} - -- (id) initWithUnzFileStruct:(unzFile)unzFile fileNameInZip:(NSString *)fileNameInZip; +#pragma mark - +#pragma mark Reading data - (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer; - (void) finishedReading; + @end diff --git a/Objective-Zip/OZZipReadStream.m b/Objective-Zip/OZZipReadStream.m index 93d9f0d..0f99ce3 100644 --- a/Objective-Zip/OZZipReadStream.m +++ b/Objective-Zip/OZZipReadStream.m @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 28/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -32,15 +32,35 @@ // #import "OZZipReadStream.h" +#import "OZZipReadStream+Internals.h" #import "OZZipException.h" +#import "OZZipException+Internals.h" -#include "unzip.h" +#pragma mark - +#pragma mark OZZipReadStream extension + +@interface OZZipReadStream () { + NSString *_fileNameInZip; + +@private + unzFile _unzFile; +} + + +@end + + +#pragma mark - +#pragma mark OZZipReadStream implementation @implementation OZZipReadStream -- (id) initWithUnzFileStruct:(unzFile)unzFile fileNameInZip:(NSString *)fileNameInZip { +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithUnzFileStruct:(unzFile)unzFile fileNameInZip:(NSString *)fileNameInZip { if (self= [super init]) { _unzFile= unzFile; _fileNameInZip= fileNameInZip; @@ -49,6 +69,10 @@ return self; } + +#pragma mark - +#pragma mark Reading data + - (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer { int err= unzReadCurrentFile(_unzFile, [buffer mutableBytes], [buffer length]); if (err < 0) { diff --git a/Objective-Zip/OZZipWriteStream+Internals.h b/Objective-Zip/OZZipWriteStream+Internals.h new file mode 100644 index 0000000..99970ff --- /dev/null +++ b/Objective-Zip/OZZipWriteStream+Internals.h @@ -0,0 +1,48 @@ +// +// OZZipWriteStream+Internals.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#import "OZZipWriteStream.h" + +#include "zip.h" + + +@interface OZZipWriteStream (Internals) + + +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithZipFileStruct:(zipFile)zipFile fileNameInZip:(NSString *)fileNameInZip; + + +@end diff --git a/Objective-Zip/OZZipWriteStream.h b/Objective-Zip/OZZipWriteStream.h index bd95391..cd293e5 100644 --- a/Objective-Zip/OZZipWriteStream.h +++ b/Objective-Zip/OZZipWriteStream.h @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -33,19 +33,15 @@ #import -#include "zip.h" + +@interface OZZipWriteStream : NSObject -@interface OZZipWriteStream : NSObject { - NSString *_fileNameInZip; - -@private - zipFile _zipFile; -} - -- (id) initWithZipFileStruct:(zipFile)zipFile fileNameInZip:(NSString *)fileNameInZip; +#pragma mark - +#pragma mark Writing data - (void) writeData:(NSData *)data; - (void) finishedWriting; + @end diff --git a/Objective-Zip/OZZipWriteStream.m b/Objective-Zip/OZZipWriteStream.m index bbd064c..0ff252c 100644 --- a/Objective-Zip/OZZipWriteStream.m +++ b/Objective-Zip/OZZipWriteStream.m @@ -3,7 +3,7 @@ // Objective-Zip v. 0.8.3 // // Created by Gianluca Bertani on 25/12/09. -// Copyright 2009-10 Flying Dolphin Studio. All rights reserved. +// Copyright 2009-2015 Gianluca Bertani. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -32,15 +32,35 @@ // #import "OZZipWriteStream.h" +#import "OZZipWriteStream+Internals.h" #import "OZZipException.h" +#import "OZZipException+Internals.h" -#include "zip.h" +#pragma mark - +#pragma mark OZZipWriteStream extension + +@interface OZZipWriteStream () { + NSString *_fileNameInZip; + +@private + zipFile _zipFile; +} + + +@end + + +#pragma mark - +#pragma mark OZZipWriteStream implementation @implementation OZZipWriteStream -- (id) initWithZipFileStruct:(zipFile)zipFile fileNameInZip:(NSString *)fileNameInZip { +#pragma mark - +#pragma mark Initialization + +- (instancetype) initWithZipFileStruct:(zipFile)zipFile fileNameInZip:(NSString *)fileNameInZip { if (self= [super init]) { _zipFile= zipFile; _fileNameInZip= fileNameInZip; @@ -49,6 +69,10 @@ return self; } + +#pragma mark - +#pragma mark Writing data + - (void) writeData:(NSData *)data { int err= zipWriteInFileInZip(_zipFile, [data bytes], [data length]); if (err < 0) { diff --git a/Objective-Zip/Objective-Zip.h b/Objective-Zip/Objective-Zip.h new file mode 100644 index 0000000..b0958a5 --- /dev/null +++ b/Objective-Zip/Objective-Zip.h @@ -0,0 +1,40 @@ +// +// Objective-Zip.h +// Objective-Zip v. 0.8.3 +// +// Created by Gianluca Bertani on 27/08/15. +// Copyright 2009-2015 Gianluca Bertani. 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 Gianluca Bertani 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 OWNER 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. +// + +#import "OZZipFile.h" +#import "OZZipFileMode.h" +#import "OZZipCompressionLevel.h" +#import "OZZipException.h" +#import "OZZipWriteStream.h" +#import "OZZipReadStream.h" +#import "OZFileInZipInfo.h"