ObjFW  Check-in [34e0dcaf87]

Overview
Comment:OFZIP: Add GZIP support
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 34e0dcaf87b597c482621f9ce2c37a1db1bf0dcd19d5a8a0d4b5aa4f45d5e172
User & Date: js on 2016-04-24 02:32:33
Other Links: manifest | tags
Context
2016-04-24
11:13
OFZIP: Small change to make GCC happy check-in: a4af1a1447 user: js tags: trunk
02:32
OFZIP: Add GZIP support check-in: 34e0dcaf87 user: js tags: trunk
02:01
Refactor OFZIP check-in: 4300366b0b user: js tags: trunk
Changes

Added utils/ofzip/GZIPArchive.h version [547a107998].



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
 *   Jonathan Schleifer <js@heap.zone>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFGZIPStream.h"

#import "Archive.h"

@interface GZIPArchive: OFObject <Archive>
{
	OFGZIPStream *_stream;
}
@end

Added utils/ofzip/GZIPArchive.m version [2572905d24].





















































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
 *   Jonathan Schleifer <js@heap.zone>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFApplication.h"
#import "OFStdIOStream.h"

#import "GZIPArchive.h"
#import "OFZIP.h"

static OFZIP *app;

@implementation GZIPArchive
+ (void)initialize
{
	if (self == [GZIPArchive class])
		app = [[OFApplication sharedApplication] delegate];
}

+ (instancetype)archiveWithFile: (OFFile*)file
{
	return [[[self alloc] initWithFile: file] autorelease];
}

- initWithFile: (OFFile*)file
{
	self = [super init];

	@try {
		_stream = [[OFGZIPStream alloc] initWithStream: file];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_stream release];

	[super dealloc];
}

- (void)listFiles
{
	[of_stderr writeLine: @"Cannot list files of a .gz archive!"];
	app->_exitStatus = 1;
}

- (void)extractFiles: (OFArray OF_GENERIC(OFString*)*)files
{
	OFString *fileName;
	OFFile *output;

	if ([files count] != 0) {
		[of_stderr writeLine:
		    @"Cannot extract a specific file of a .gz archive!"];
		app->_exitStatus = 1;
		return;
	}

	fileName = [[app->_archivePath lastPathComponent]
	    stringByDeletingPathExtension];

	if (app->_outputLevel >= 0)
		[of_stdout writeFormat: @"Extracting %@...", fileName];

	if (![app shouldExtractFile: fileName
			outFileName: fileName])
		return;

	output = [OFFile fileWithPath: fileName
				 mode: @"wb"];
	/* TODO: Copy permissions */

	while (![_stream isAtEndOfStream]) {
		ssize_t length = [app copyBlockFromStream: _stream
						 toStream: output
						 fileName: fileName];

		if (length < 0) {
			app->_exitStatus = 1;
			return;
		}
	}

	if (app->_outputLevel >= 0)
		[of_stdout writeFormat: @"\rExtracting %@... done\n", fileName];
}
@end

Modified utils/ofzip/Makefile from [dc2fb4a588] to [115d091be2].

1
2
3

4
5
6
7
8
9
10
11
12
13
14
include ../../extra.mk

PROG = ofzip${PROG_SUFFIX}

SRCS = OFZIP.m		\
       ZIPArchive.m

include ../../buildsys.mk

${PROG}: ${LIBOBJFW_DEP_LVL2}

CPPFLAGS += -I../../src -I../../src/runtime -I../../src/exceptions -I../..
LIBS := -L../../src -lobjfw ${LIBS}
LD = ${OBJC}
LDFLAGS += ${LDFLAGS_RPATH}



>
|










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include ../../extra.mk

PROG = ofzip${PROG_SUFFIX}
SRCS = GZIPArchive.m	\
       OFZIP.m		\
       ZIPArchive.m

include ../../buildsys.mk

${PROG}: ${LIBOBJFW_DEP_LVL2}

CPPFLAGS += -I../../src -I../../src/runtime -I../../src/exceptions -I../..
LIBS := -L../../src -lobjfw ${LIBS}
LD = ${OBJC}
LDFLAGS += ${LDFLAGS_RPATH}

Modified utils/ofzip/OFZIP.h from [991ecbd6f2] to [b8cc3eb658].

11
12
13
14
15
16
17

18
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"


#import "Archive.h"

@interface OFZIP: OFObject
{
	int8_t _override;
@public
	int8_t _outputLevel;

	int _exitStatus;
}

- (id <Archive>)openArchiveWithPath: (OFString*)path;
- (bool)shouldExtractFile: (OFString*)fileName
	      outFileName: (OFString*)outFileName;
- (ssize_t)copyBlockFromStream: (OFStream*)input
		      toStream: (OFStream*)output
		      fileName: (OFString*)fileName;
@end







>








>










11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"
#import "OFString.h"

#import "Archive.h"

@interface OFZIP: OFObject
{
	int8_t _override;
@public
	int8_t _outputLevel;
	OFString *_archivePath;
	int _exitStatus;
}

- (id <Archive>)openArchiveWithPath: (OFString*)path;
- (bool)shouldExtractFile: (OFString*)fileName
	      outFileName: (OFString*)outFileName;
- (ssize_t)copyBlockFromStream: (OFStream*)input
		      toStream: (OFStream*)output
		      fileName: (OFString*)fileName;
@end

Modified utils/ofzip/OFZIP.m from [f64cd875b8] to [ca3f3e8461].

23
24
25
26
27
28
29

30
31
32
33
34
35
36
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFOptionsParser.h"
#import "OFStdIOStream.h"

#import "OFZIP.h"
#import "ZIPArchive.h"


#import "OFCreateDirectoryFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"








>







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFOptionsParser.h"
#import "OFStdIOStream.h"

#import "OFZIP.h"
#import "ZIPArchive.h"
#import "GZIPArchive.h"

#import "OFCreateDirectoryFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"

195
196
197
198
199
200
201



202
203
204
205
206
207
208
209
210
211



212
213
214
215
216
217
218
219
}

- (id <Archive>)openArchiveWithPath: (OFString*)path
{
	OFFile *file;
	id <Archive> archive;




	@try {
		file = [OFFile fileWithPath: path
				       mode: @"rb"];
	} @catch (OFOpenItemFailedException *e) {
		[of_stderr writeFormat: @"Failed to open file %@: %s\n",
					[e path], strerror([e errNo])];
		[OFApplication terminateWithStatus: 1];
	}

	@try {



		archive = [ZIPArchive archiveWithFile: file];
	} @catch (OFReadFailedException *e) {
		[of_stderr writeFormat: @"Failed to read file %@: %s\n",
					path, strerror([e errNo])];
		[OFApplication terminateWithStatus: 1];
	} @catch (OFInvalidFormatException *e) {
		[of_stderr writeFormat: @"File %@ is not a valid archive!\n",
					path];







>
>
>










>
>
>
|







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
}

- (id <Archive>)openArchiveWithPath: (OFString*)path
{
	OFFile *file;
	id <Archive> archive;

	[_archivePath release];
	_archivePath = [path copy];

	@try {
		file = [OFFile fileWithPath: path
				       mode: @"rb"];
	} @catch (OFOpenItemFailedException *e) {
		[of_stderr writeFormat: @"Failed to open file %@: %s\n",
					[e path], strerror([e errNo])];
		[OFApplication terminateWithStatus: 1];
	}

	@try {
		if ([path hasSuffix: @".gz"])
			archive = [GZIPArchive archiveWithFile: file];
		else
			archive = [ZIPArchive archiveWithFile: file];
	} @catch (OFReadFailedException *e) {
		[of_stderr writeFormat: @"Failed to read file %@: %s\n",
					path, strerror([e errNo])];
		[OFApplication terminateWithStatus: 1];
	} @catch (OFInvalidFormatException *e) {
		[of_stderr writeFormat: @"File %@ is not a valid archive!\n",
					path];