summaryrefslogtreecommitdiff
path: root/archive-zip.c
diff options
context:
space:
mode:
Diffstat (limited to 'archive-zip.c')
-rw-r--r--archive-zip.c43
1 files changed, 16 insertions, 27 deletions
diff --git a/archive-zip.c b/archive-zip.c
index 55f66b4060..b2c4fe0e9f 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -111,8 +111,9 @@ static void copy_le32(unsigned char *dest, unsigned int n)
dest[3] = 0xff & (n >> 030);
}
-static void *zlib_deflate(void *data, unsigned long size,
- int compression_level, unsigned long *compressed_size)
+static void *zlib_deflate_raw(void *data, unsigned long size,
+ int compression_level,
+ unsigned long *compressed_size)
{
git_zstream stream;
unsigned long maxsize;
@@ -120,7 +121,7 @@ static void *zlib_deflate(void *data, unsigned long size,
int result;
memset(&stream, 0, sizeof(stream));
- git_deflate_init(&stream, compression_level);
+ git_deflate_init_raw(&stream, compression_level);
maxsize = git_deflate_bound(&stream, size);
buffer = xmalloc(maxsize);
@@ -240,7 +241,6 @@ static int write_zip_entry(struct archiver_args *args,
(mode & 0111) ? ((mode) << 16) : 0;
if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
method = 8;
- compressed_size = size;
if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
size > big_file_threshold) {
@@ -259,20 +259,18 @@ static int write_zip_entry(struct archiver_args *args,
crc = crc32(crc, buffer, size);
out = buffer;
}
+ compressed_size = (method == 0) ? size : 0;
} else {
return error("unsupported file mode: 0%o (SHA1: %s)", mode,
sha1_to_hex(sha1));
}
if (buffer && method == 8) {
- deflated = zlib_deflate(buffer, size, args->compression_level,
- &compressed_size);
- if (deflated && compressed_size - 6 < size) {
- /* ZLIB --> raw compressed data (see RFC 1950) */
- /* CMF and FLG ... */
- out = (unsigned char *)deflated + 2;
- compressed_size -= 6; /* ... and ADLER32 */
- } else {
+ out = deflated = zlib_deflate_raw(buffer, size,
+ args->compression_level,
+ &compressed_size);
+ if (!out || compressed_size >= size) {
+ out = buffer;
method = 0;
compressed_size = size;
}
@@ -313,10 +311,7 @@ static int write_zip_entry(struct archiver_args *args,
copy_le16(header.compression_method, method);
copy_le16(header.mtime, zip_time);
copy_le16(header.mdate, zip_date);
- if (flags & ZIP_STREAM)
- set_zip_header_data_desc(&header, 0, 0, 0);
- else
- set_zip_header_data_desc(&header, size, compressed_size, crc);
+ set_zip_header_data_desc(&header, size, compressed_size, crc);
copy_le16(header.filename_length, pathlen);
copy_le16(header.extra_length, ZIP_EXTRA_MTIME_SIZE);
write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
@@ -356,7 +351,7 @@ static int write_zip_entry(struct archiver_args *args,
unsigned char compressed[STREAM_BUFFER_SIZE * 2];
memset(&zstream, 0, sizeof(zstream));
- git_deflate_init(&zstream, args->compression_level);
+ git_deflate_init_raw(&zstream, args->compression_level);
compressed_size = 0;
zstream.next_out = compressed;
@@ -373,13 +368,10 @@ static int write_zip_entry(struct archiver_args *args,
result = git_deflate(&zstream, 0);
if (result != Z_OK)
die("deflate error (%d)", result);
- out = compressed;
- if (!compressed_size)
- out += 2;
- out_len = zstream.next_out - out;
+ out_len = zstream.next_out - compressed;
if (out_len > 0) {
- write_or_die(1, out, out_len);
+ write_or_die(1, compressed, out_len);
compressed_size += out_len;
zstream.next_out = compressed;
zstream.avail_out = sizeof(compressed);
@@ -397,11 +389,8 @@ static int write_zip_entry(struct archiver_args *args,
die("deflate error (%d)", result);
git_deflate_end(&zstream);
- out = compressed;
- if (!compressed_size)
- out += 2;
- out_len = zstream.next_out - out - 4;
- write_or_die(1, out, out_len);
+ out_len = zstream.next_out - compressed;
+ write_or_die(1, compressed, out_len);
compressed_size += out_len;
zip_offset += compressed_size;