Use compression flags when compressing.
Document bzip2.
diff --git a/lib/zip_close.c b/lib/zip_close.c
index f20b019..3180e15 100644
--- a/lib/zip_close.c
+++ b/lib/zip_close.c
@@ -389,8 +389,7 @@
}
if (needs_compress) {
- /* TODO: compression flags */
- if ((src_tmp = zip_source_compress(za, src_final, de->comp_method, 0)) == NULL) {
+ if ((src_tmp = zip_source_compress(za, src_final, de->comp_method, de->compression_level)) == NULL) {
zip_source_free(src_final);
return -1;
}
diff --git a/lib/zip_dirent.c b/lib/zip_dirent.c
index 197bb3b..a369900 100644
--- a/lib/zip_dirent.c
+++ b/lib/zip_dirent.c
@@ -296,6 +296,7 @@
de->int_attrib = 0;
de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT;
de->offset = 0;
+ de->compression_level = 0;
de->encryption_method = ZIP_EM_NONE;
de->password = NULL;
}
diff --git a/lib/zip_set_file_compression.c b/lib/zip_set_file_compression.c
index 5321687..a35290d 100644
--- a/lib/zip_set_file_compression.c
+++ b/lib/zip_set_file_compression.c
@@ -41,7 +41,7 @@
zip_entry_t *e;
zip_int32_t old_method;
- if (idx >= za->nentry) {
+ if (idx >= za->nentry || flags > 9) {
zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
@@ -60,11 +60,15 @@
old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method);
- /* TODO: revisit this when flags are supported, since they may require a recompression */
+ /* TODO: do we want to recompress if level is set? Only if it's
+ * different than what bit flags tell us, but those are not
+ * defined for all compression methods, or not directly mappable
+ * to levels */
if (method == old_method) {
if (e->changes) {
e->changes->changed &= ~ZIP_DIRENT_COMP_METHOD;
+ e->changes->compression_level = 0;
if (e->changes->changed == 0) {
_zip_dirent_free(e->changes);
e->changes = NULL;
@@ -80,6 +84,7 @@
}
e->changes->comp_method = method;
+ e->changes->compression_level = (zip_uint16_t)flags;
e->changes->changed |= ZIP_DIRENT_COMP_METHOD;
}
diff --git a/lib/zipint.h b/lib/zipint.h
index 0e725b2..d4937d1 100644
--- a/lib/zipint.h
+++ b/lib/zipint.h
@@ -283,6 +283,7 @@
zip_uint32_t ext_attrib; /* (c) external file attributes */
zip_uint64_t offset; /* (c) offset of local header */
+ zip_uint16_t compression_level; /* level of compression to use (never valid in orig) */
zip_uint16_t encryption_method; /* encryption method, computed from other fields */
char *password; /* file specific encryption password */
};
diff --git a/man/zip_set_file_compression.mdoc b/man/zip_set_file_compression.mdoc
index 7924aeb..65e02eb 100644
--- a/man/zip_set_file_compression.mdoc
+++ b/man/zip_set_file_compression.mdoc
@@ -1,5 +1,5 @@
.\" zip_set_file_compression.mdoc -- set compression method and its flags
-.\" Copyright (C) 2012-2016 Dieter Baron and Thomas Klausner
+.\" Copyright (C) 2012-2017 Dieter Baron and Thomas Klausner
.\"
.\" This file is part of libzip, a library to manipulate ZIP files.
.\" The authors can be contacted at <libzip@nih.at>
@@ -29,7 +29,7 @@
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd May 1, 2012
+.Dd April 6, 2017
.Dt ZIP_SET_FILE_COMPRESSION 3
.Os
.Sh NAME
@@ -63,21 +63,26 @@
.Dv ZIP_CM_DEFLATE .
.It Dv ZIP_CM_STORE
Store the file uncompressed.
+.It Dv ZIP_CM_BZIP2
+Compress the file using the
+.Xr bzip2 1
+algorithm.
.It Dv ZIP_CM_DEFLATE
Deflate the file with the
.Xr zlib 3
algorithm and default options
.El
.Pp
+.Em NOTE :
+Only the deflate and store methods can be assumed to be universally
+supported.
+.Pp
The
.Ar comp_flags
-argument is currently ignored.
-.\" For the comp_flags argument, the lower 4 bits define the compression
-.\" level.
-.\" 0 is fastest compression, 15 is highest compression
-.\" .Dv ZIP_COMP_FL_DEFAULT
-.\" can be used to specify that the default shall be used.
-.\" Further compression method specific flags will be added over time.
+argument defines the compression level, 1 being fastest compression
+and 9 highest.
+Allowed values are 1-9, other values are undefined.
+Further compression method specific flags might be added over time.
.Pp
The current compression method for a file in a zip archive can be
determined using