2020-06-09 13:55:08 +02:00
|
|
|
/* SOGoZipArchiver.m - this file is part of SOGo
|
2020-06-09 13:50:29 +02:00
|
|
|
*
|
2020-06-09 13:55:08 +02:00
|
|
|
* Copyright (C) 2020 Inverse inc.
|
2020-06-09 13:50:29 +02:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSCharacterSet.h>
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
|
|
|
|
#import "SOGoZipArchiver.h"
|
|
|
|
|
|
|
|
@implementation SOGoZipArchiver
|
|
|
|
|
|
|
|
+ (id)archiverAtPath:(NSString *)file
|
|
|
|
{
|
|
|
|
id newArchiver = [[self alloc] initFromFile: file];
|
|
|
|
[newArchiver autorelease];
|
|
|
|
return newArchiver;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2020-06-19 17:07:07 +02:00
|
|
|
z = NULL;
|
2020-06-09 13:50:29 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initFromFile:(NSString *)file
|
|
|
|
{
|
|
|
|
id ret;
|
|
|
|
|
|
|
|
ret = nil;
|
|
|
|
if (file) {
|
|
|
|
if ((self = [self init])) {
|
|
|
|
int errorp;
|
2020-06-19 17:07:07 +02:00
|
|
|
self->z = zip_open([file cString], ZIP_CREATE | ZIP_EXCL, &errorp);
|
|
|
|
if (self->z == NULL) {
|
2020-06-23 18:06:10 +02:00
|
|
|
#if defined(LIBZIP_VERSION_MAJOR) && LIBZIP_VERSION_MAJOR >= 1
|
|
|
|
zip_error_t ziperror;
|
|
|
|
zip_error_init_with_code(&ziperror, errorp);
|
|
|
|
NSLog(@"Failed to open zip output file %@: %@", file,
|
|
|
|
[NSString stringWithCString: zip_error_strerror(&ziperror)]);
|
|
|
|
#else
|
|
|
|
NSLog(@"Failed to open zip output file %@", file);
|
|
|
|
#endif
|
2020-06-09 13:50:29 +02:00
|
|
|
} else {
|
|
|
|
ret = self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)putFileWithName:(NSString *)filename andData:(NSData *)data
|
|
|
|
{
|
2020-06-19 17:07:07 +02:00
|
|
|
if (self->z == NULL) {
|
2020-06-09 13:50:29 +02:00
|
|
|
NSLog(@"Failed to add file, archive is not open");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2020-06-19 17:30:42 +02:00
|
|
|
struct zip_source *source = zip_source_buffer(self->z, [data bytes], [data length], 0);
|
2020-06-09 13:50:29 +02:00
|
|
|
if (source == NULL) {
|
2020-06-19 17:07:07 +02:00
|
|
|
NSLog(@"Failed to create zip source from buffer: %@", [NSString stringWithCString: zip_strerror(self->z)]);
|
2020-06-09 13:50:29 +02:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2020-06-19 17:30:42 +02:00
|
|
|
#ifdef ZIP_FL_ENC_UTF_8
|
2020-06-19 17:07:07 +02:00
|
|
|
if (zip_file_add(self->z, [filename UTF8String], source, ZIP_FL_ENC_UTF_8) < 0) {
|
|
|
|
NSLog(@"Failed to add file %@: %@", filename, [NSString stringWithCString: zip_strerror(self->z)]);
|
2020-06-09 13:50:29 +02:00
|
|
|
zip_source_free(source);
|
|
|
|
}
|
2020-06-19 17:30:42 +02:00
|
|
|
#else
|
|
|
|
if (zip_add(self->z, [filename UTF8String], source) < 0) {
|
|
|
|
NSLog(@"Failed to add file %@: %@", filename, [NSString stringWithCString: zip_strerror(self->z)]);
|
|
|
|
zip_source_free(source);
|
|
|
|
}
|
|
|
|
#endif
|
2020-06-09 13:50:29 +02:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)close
|
|
|
|
{
|
|
|
|
BOOL success = YES;
|
2020-06-19 17:07:07 +02:00
|
|
|
if (self->z != NULL) {
|
|
|
|
if (zip_close(self->z) != 0) {
|
|
|
|
NSLog(@"Failed to close zip archive: %@", [NSString stringWithCString: zip_strerror(self->z)]);
|
2020-06-23 18:06:10 +02:00
|
|
|
#if defined(LIBZIP_VERSION_MAJOR) && (LIBZIP_VERSION_MAJOR >= 1 || LIBZIP_VERSION_MINOR >= 11)
|
|
|
|
zip_discard(self->z);
|
|
|
|
#endif
|
2020-06-09 13:50:29 +02:00
|
|
|
success = NO;
|
|
|
|
}
|
2020-06-19 17:07:07 +02:00
|
|
|
self->z = NULL;
|
2020-06-09 13:50:29 +02:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|