Skip to content

Commit 025d07e

Browse files
committed
Use compression flags when compressing.
Document bzip2.
1 parent 641ed7f commit 025d07e

5 files changed

Lines changed: 24 additions & 13 deletions

File tree

lib/zip_close.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de, progress_state_t *progr
389389
}
390390

391391
if (needs_compress) {
392-
/* TODO: compression flags */
393-
if ((src_tmp = zip_source_compress(za, src_final, de->comp_method, 0)) == NULL) {
392+
if ((src_tmp = zip_source_compress(za, src_final, de->comp_method, de->compression_level)) == NULL) {
394393
zip_source_free(src_final);
395394
return -1;
396395
}

lib/zip_dirent.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ _zip_dirent_init(zip_dirent_t *de)
296296
de->int_attrib = 0;
297297
de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT;
298298
de->offset = 0;
299+
de->compression_level = 0;
299300
de->encryption_method = ZIP_EM_NONE;
300301
de->password = NULL;
301302
}

lib/zip_set_file_compression.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_ui
4141
zip_entry_t *e;
4242
zip_int32_t old_method;
4343

44-
if (idx >= za->nentry) {
44+
if (idx >= za->nentry || flags > 9) {
4545
zip_error_set(&za->error, ZIP_ER_INVAL, 0);
4646
return -1;
4747
}
@@ -60,11 +60,15 @@ zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_ui
6060

6161
old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method);
6262

63-
/* TODO: revisit this when flags are supported, since they may require a recompression */
63+
/* TODO: do we want to recompress if level is set? Only if it's
64+
* different than what bit flags tell us, but those are not
65+
* defined for all compression methods, or not directly mappable
66+
* to levels */
6467

6568
if (method == old_method) {
6669
if (e->changes) {
6770
e->changes->changed &= ~ZIP_DIRENT_COMP_METHOD;
71+
e->changes->compression_level = 0;
6872
if (e->changes->changed == 0) {
6973
_zip_dirent_free(e->changes);
7074
e->changes = NULL;
@@ -80,6 +84,7 @@ zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_ui
8084
}
8185

8286
e->changes->comp_method = method;
87+
e->changes->compression_level = (zip_uint16_t)flags;
8388
e->changes->changed |= ZIP_DIRENT_COMP_METHOD;
8489
}
8590

lib/zipint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ struct zip_dirent {
283283
zip_uint32_t ext_attrib; /* (c) external file attributes */
284284
zip_uint64_t offset; /* (c) offset of local header */
285285

286+
zip_uint16_t compression_level; /* level of compression to use (never valid in orig) */
286287
zip_uint16_t encryption_method; /* encryption method, computed from other fields */
287288
char *password; /* file specific encryption password */
288289
};

man/zip_set_file_compression.mdoc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" zip_set_file_compression.mdoc -- set compression method and its flags
2-
.\" Copyright (C) 2012-2016 Dieter Baron and Thomas Klausner
2+
.\" Copyright (C) 2012-2017 Dieter Baron and Thomas Klausner
33
.\"
44
.\" This file is part of libzip, a library to manipulate ZIP files.
55
.\" The authors can be contacted at <libzip@nih.at>
@@ -29,7 +29,7 @@
2929
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
3030
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
.\"
32-
.Dd May 1, 2012
32+
.Dd April 6, 2017
3333
.Dt ZIP_SET_FILE_COMPRESSION 3
3434
.Os
3535
.Sh NAME
@@ -63,21 +63,26 @@ default compression; currently the same as
6363
.Dv ZIP_CM_DEFLATE .
6464
.It Dv ZIP_CM_STORE
6565
Store the file uncompressed.
66+
.It Dv ZIP_CM_BZIP2
67+
Compress the file using the
68+
.Xr bzip2 1
69+
algorithm.
6670
.It Dv ZIP_CM_DEFLATE
6771
Deflate the file with the
6872
.Xr zlib 3
6973
algorithm and default options
7074
.El
7175
.Pp
76+
.Em NOTE :
77+
Only the deflate and store methods can be assumed to be universally
78+
supported.
79+
.Pp
7280
The
7381
.Ar comp_flags
74-
argument is currently ignored.
75-
.\" For the comp_flags argument, the lower 4 bits define the compression
76-
.\" level.
77-
.\" 0 is fastest compression, 15 is highest compression
78-
.\" .Dv ZIP_COMP_FL_DEFAULT
79-
.\" can be used to specify that the default shall be used.
80-
.\" Further compression method specific flags will be added over time.
82+
argument defines the compression level, 1 being fastest compression
83+
and 9 highest.
84+
Allowed values are 1-9, other values are undefined.
85+
Further compression method specific flags might be added over time.
8186
.Pp
8287
The current compression method for a file in a zip archive can be
8388
determined using

0 commit comments

Comments
 (0)