diff options
author | mpeterv <mpeterval@gmail.com> | 2016-02-19 11:16:35 +0300 |
---|---|---|
committer | mpeterv <mpeterval@gmail.com> | 2016-02-19 11:17:30 +0300 |
commit | c2bad31e7fea154c3fdee453b2d774f48b422be8 (patch) | |
tree | 94e7f0de1571446fcafe728fe20a9682dc850699 /src | |
parent | 204d6cc98fb275865c28653b6eed57060e84255e (diff) | |
download | luarocks-c2bad31e7fea154c3fdee453b2d774f48b422be8.tar.gz luarocks-c2bad31e7fea154c3fdee453b2d774f48b422be8.tar.bz2 luarocks-c2bad31e7fea154c3fdee453b2d774f48b422be8.zip |
Compatibility with lua-zlib
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/tools/zip.lua | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/src/luarocks/tools/zip.lua b/src/luarocks/tools/zip.lua index 101cae82..e6d9e36a 100644 --- a/src/luarocks/tools/zip.lua +++ b/src/luarocks/tools/zip.lua | |||
@@ -1,13 +1,35 @@ | |||
1 | 1 | ||
2 | --- A Lua implementation of .zip file archiving (used for creating .rock files), | 2 | --- A Lua implementation of .zip file archiving (used for creating .rock files), |
3 | -- using only lzlib. | 3 | -- using only lzlib or lua-lzib. |
4 | --module("luarocks.tools.zip", package.seeall) | ||
5 | local zip = {} | 4 | local zip = {} |
6 | 5 | ||
7 | local zlib = require("zlib") | 6 | local zlib = require("zlib") |
8 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
9 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
10 | 9 | ||
10 | -- zlib module can be provided by both lzlib and lua-lzib packages. | ||
11 | -- Create a compatibility layer. | ||
12 | local zlib_compress, zlib_crc32 | ||
13 | if zlib._VERSION:match "^lua%-zlib" then | ||
14 | function zlib_compress(data) | ||
15 | return (zlib.deflate()(data, "finish")) | ||
16 | end | ||
17 | |||
18 | function zlib_crc32(data) | ||
19 | return zlib.crc32()(data) | ||
20 | end | ||
21 | elseif zlib._VERSION:match "^lzlib" then | ||
22 | function zlib_compress(data) | ||
23 | return zlib.compress(data) | ||
24 | end | ||
25 | |||
26 | function zlib_crc32(data) | ||
27 | return zlib.crc32(zlib.crc32(), data) | ||
28 | end | ||
29 | else | ||
30 | error("unknown zlib library", 0) | ||
31 | end | ||
32 | |||
11 | local function number_to_bytestring(number, nbytes) | 33 | local function number_to_bytestring(number, nbytes) |
12 | local out = {} | 34 | local out = {} |
13 | for _ = 1, nbytes do | 35 | for _ = 1, nbytes do |
@@ -31,32 +53,28 @@ local function zipwriter_open_new_file_in_zip(self, filename) | |||
31 | self.local_file_header = lfh | 53 | self.local_file_header = lfh |
32 | lfh.last_mod_file_time = 0 -- TODO | 54 | lfh.last_mod_file_time = 0 -- TODO |
33 | lfh.last_mod_file_date = 0 -- TODO | 55 | lfh.last_mod_file_date = 0 -- TODO |
34 | lfh.crc32 = 0 -- initial value | ||
35 | lfh.compressed_size = 0 -- unknown yet | ||
36 | lfh.uncompressed_size = 0 -- unknown yet | ||
37 | lfh.file_name_length = #filename | 56 | lfh.file_name_length = #filename |
38 | lfh.extra_field_length = 0 | 57 | lfh.extra_field_length = 0 |
39 | lfh.file_name = filename:gsub("\\", "/") | 58 | lfh.file_name = filename:gsub("\\", "/") |
40 | lfh.external_attr = 0 -- TODO properly store permissions | 59 | lfh.external_attr = 0 -- TODO properly store permissions |
41 | self.in_open_file = true | 60 | self.in_open_file = true |
42 | self.data = {} | ||
43 | return true | 61 | return true |
44 | end | 62 | end |
45 | 63 | ||
46 | --- Write data to the file currently being stored in the zipfile. | 64 | --- Write data to the file currently being stored in the zipfile. |
47 | -- @param self handle of the zipfile being written. | 65 | -- @param self handle of the zipfile being written. |
48 | -- @param buf string containing data to be written. | 66 | -- @param data string containing full contents of the file. |
49 | -- @return true if succeeded, nil in case of failure. | 67 | -- @return true if succeeded, nil in case of failure. |
50 | local function zipwriter_write_file_in_zip(self, buf) | 68 | local function zipwriter_write_file_in_zip(self, data) |
51 | if not self.in_open_file then | 69 | if not self.in_open_file then |
52 | return nil | 70 | return nil |
53 | end | 71 | end |
54 | local lfh = self.local_file_header | 72 | local lfh = self.local_file_header |
55 | local cbuf = zlib.compress(buf):sub(3, -5) | 73 | local compressed = zlib_compress(data):sub(3, -5) |
56 | lfh.crc32 = zlib.crc32(lfh.crc32, buf) | 74 | lfh.crc32 = zlib_crc32(data) |
57 | lfh.compressed_size = lfh.compressed_size + #cbuf | 75 | lfh.compressed_size = #compressed |
58 | lfh.uncompressed_size = lfh.uncompressed_size + #buf | 76 | lfh.uncompressed_size = #data |
59 | table.insert(self.data, cbuf) | 77 | self.data = compressed |
60 | return true | 78 | return true |
61 | end | 79 | end |
62 | 80 | ||
@@ -86,10 +104,8 @@ local function zipwriter_close_file_in_zip(self) | |||
86 | zh:write(number_to_bytestring(lfh.extra_field_length, 2)) | 104 | zh:write(number_to_bytestring(lfh.extra_field_length, 2)) |
87 | zh:write(lfh.file_name) | 105 | zh:write(lfh.file_name) |
88 | 106 | ||
89 | -- File data | 107 | -- File data |
90 | for _, cbuf in ipairs(self.data) do | 108 | zh:write(self.data) |
91 | zh:write(cbuf) | ||
92 | end | ||
93 | 109 | ||
94 | -- Data descriptor | 110 | -- Data descriptor |
95 | zh:write(number_to_bytestring(lfh.crc32, 4)) | 111 | zh:write(number_to_bytestring(lfh.crc32, 4)) |
@@ -117,12 +133,12 @@ local function zipwriter_add(self, file) | |||
117 | end | 133 | end |
118 | end | 134 | end |
119 | if ok then | 135 | if ok then |
120 | local buf = fin:read("*a") | 136 | local data = fin:read("*a") |
121 | if not buf then | 137 | if not data then |
122 | err = "error reading "..file | 138 | err = "error reading "..file |
123 | ok = false | 139 | ok = false |
124 | else | 140 | else |
125 | ok = self:write_file_in_zip(buf) | 141 | ok = self:write_file_in_zip(data) |
126 | if not ok then | 142 | if not ok then |
127 | err = "error in writing "..file.." in the zipfile" | 143 | err = "error in writing "..file.." in the zipfile" |
128 | end | 144 | end |