seven_zip_ruby 1.0.0-x64-mingw32 → 1.1.0-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -6
- data/lib/seven_zip_ruby/exception.rb +3 -0
- data/lib/seven_zip_ruby/seven_zip_reader.rb +298 -60
- data/lib/seven_zip_ruby/seven_zip_writer.rb +195 -6
- data/lib/seven_zip_ruby/version.rb +1 -1
- data/spec/seven_zip_ruby_spec.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6bed3467dce41b8cfaa3806096bf6b2f4a3b5ae
|
4
|
+
data.tar.gz: d7daca75c57027da0f9b4f1b164beaff81efdc07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fc2297a302e79898972e45208afee9edadfec80601755f15497c4d60b61aba88668bf8bc09d44ed8dac0d4d31435fa5fd895f97cc47fc35102f49552aacb28b
|
7
|
+
data.tar.gz: 253b962d9aeacbebe4e1919fee7dbf8334671b6229370a1ba03b00843a2a33a0f94ef0c93a75b0a1f87f059ab6b902fd6867cca90a11107a2c4f038f7d2c3024
|
data/README.md
CHANGED
@@ -12,12 +12,8 @@ This extension calls the native library, 7z.dll or 7z.so, internally and it is i
|
|
12
12
|
|
13
13
|
## Examples
|
14
14
|
|
15
|
-
**This is a beta version.**
|
16
|
-
The interfaces may be changed.
|
17
|
-
|
18
|
-
If you have any comments about interface API, let me know please.
|
19
|
-
|
20
15
|
### Extract archive
|
16
|
+
|
21
17
|
```ruby
|
22
18
|
File.open("filename.7z", "rb") do |file|
|
23
19
|
SevenZipRuby::Reader.open(file) do |szr|
|
@@ -27,6 +23,7 @@ end
|
|
27
23
|
```
|
28
24
|
|
29
25
|
You can also use handy method.
|
26
|
+
|
30
27
|
```ruby
|
31
28
|
File.open("filename.7z", "rb") do |file|
|
32
29
|
SevenZipRuby::Reader.extract_all(file, "path_to_dir")
|
@@ -34,6 +31,7 @@ end
|
|
34
31
|
```
|
35
32
|
|
36
33
|
### Show entries in archive
|
34
|
+
|
37
35
|
```ruby
|
38
36
|
File.open("filename.7z", "rb") do |file|
|
39
37
|
SevenZipRuby::Reader.open(file) do |szr|
|
@@ -45,6 +43,7 @@ end
|
|
45
43
|
```
|
46
44
|
|
47
45
|
### Extract encrypted archive
|
46
|
+
|
48
47
|
```ruby
|
49
48
|
File.open("filename.7z", "rb") do |file|
|
50
49
|
SevenZipRuby::Reader.open(file, { password: "Password String" }) do |szr|
|
@@ -53,6 +52,7 @@ File.open("filename.7z", "rb") do |file|
|
|
53
52
|
end
|
54
53
|
```
|
55
54
|
or
|
55
|
+
|
56
56
|
```ruby
|
57
57
|
File.open("filename.7z", "rb") do |file|
|
58
58
|
SevenZipRuby::Reader.extract_all(file, "path_to_dir", { password: "Password String" })
|
@@ -61,6 +61,7 @@ end
|
|
61
61
|
|
62
62
|
|
63
63
|
### Verify archive
|
64
|
+
|
64
65
|
```ruby
|
65
66
|
File.open("filename.7z", "rb") do |file|
|
66
67
|
SevenZipRuby::Reader.verify(file)
|
@@ -69,6 +70,7 @@ end
|
|
69
70
|
```
|
70
71
|
|
71
72
|
### Compress files
|
73
|
+
|
72
74
|
```ruby
|
73
75
|
File.open("filename.7z", "wb") do |file|
|
74
76
|
SevenZipRuby::Writer.open(file) do |szr|
|
@@ -77,6 +79,7 @@ File.open("filename.7z", "wb") do |file|
|
|
77
79
|
end
|
78
80
|
```
|
79
81
|
or
|
82
|
+
|
80
83
|
```ruby
|
81
84
|
File.open("filename.7z", "wb") do |file|
|
82
85
|
SevenZipRuby::Writer.add_directory(file, "dir")
|
@@ -118,10 +121,11 @@ File.open("filename.7z", "rb") do |file|
|
|
118
121
|
end
|
119
122
|
p data
|
120
123
|
# => File content is shown.
|
124
|
+
```
|
121
125
|
|
122
126
|
### Create archive manually
|
123
127
|
|
124
|
-
```
|
128
|
+
```ruby
|
125
129
|
File.open("filename.7z", "rb") do |file|
|
126
130
|
SevenZipRuby::Writer.open(file) do |szr|
|
127
131
|
szr.add_file "entry1.txt"
|
@@ -174,3 +178,10 @@ p(Time.now - start)
|
|
174
178
|
## License
|
175
179
|
LGPL and unRAR license. Please refer to LICENSE.txt.
|
176
180
|
|
181
|
+
## Releases
|
182
|
+
|
183
|
+
* 1.1.0
|
184
|
+
Raise error when wrong password is specified.
|
185
|
+
* 1.0.0
|
186
|
+
Initial release.
|
187
|
+
|
@@ -1,11 +1,100 @@
|
|
1
1
|
require("stringio")
|
2
2
|
|
3
3
|
module SevenZipRuby
|
4
|
+
|
5
|
+
# SevenZipReader reads 7zip archive and extract it.
|
6
|
+
#
|
7
|
+
# == Examples
|
8
|
+
# === Get archive information
|
9
|
+
# # Archive property
|
10
|
+
# File.open("filename.7z", "rb") do |file|
|
11
|
+
# SevenZipRuby::Reader.open(file) do |szr|
|
12
|
+
# info = szr.archive_property # Return ArchiveInfo instance.
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # Entry information
|
17
|
+
# File.open("filename.7z", "rb") do |file|
|
18
|
+
# SevenZipRuby::Reader.open(file) do |szr|
|
19
|
+
# entries = szr.entries
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# === Extract 7zip archive.
|
24
|
+
# # Extract archive
|
25
|
+
# File.open("filename.7z", "rb") do |file|
|
26
|
+
# SevenZipRuby::Reader.open(file) do |szr|
|
27
|
+
# szr.extract(:all, "path_to_dir")
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # Extract encrypted archive
|
32
|
+
# File.open("filename.7z", "rb") do |file|
|
33
|
+
# SevenZipRuby::Reader.open(file, password: "Password String") do |szr|
|
34
|
+
# szr.extract(:all, "path_to_dir")
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# # Extract only small files
|
39
|
+
# File.open("filename.7z", "rb") do |file|
|
40
|
+
# SevenZipRuby::Reader.open(file) do |szr|
|
41
|
+
# small_files = szr.entries.select{ |i| i.file? && i.size < 1024 }
|
42
|
+
# szr.extract(small_files, "path_to_dir")
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # Extract archive on memory
|
47
|
+
# archive_data = "....."
|
48
|
+
# stream = StringIO.new(archive_data)
|
49
|
+
# SevenZipRuby::Reader.open(stream) do |szr|
|
50
|
+
# entry_data = szr.extract_data(:all)
|
51
|
+
# # => [ "data", ... ]
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# === Verify archive
|
55
|
+
# File.open("filename.7z", "rb") do |file|
|
56
|
+
# SevenZipRuby::Reader.verify(file)
|
57
|
+
# # => true/false
|
58
|
+
# end
|
4
59
|
class SevenZipReader
|
5
60
|
class << self
|
6
|
-
|
61
|
+
# Open 7zip archive to read.
|
62
|
+
#
|
63
|
+
# ==== Args
|
64
|
+
# +stream+ :: Input stream to read 7zip archive. <tt>stream.seek</tt> and <tt>stream.read</tt> are needed.
|
65
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
66
|
+
#
|
67
|
+
# ==== Examples
|
68
|
+
# # Open archive
|
69
|
+
# File.open("filename.7z", "rb") do |file|
|
70
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
71
|
+
# # Read and extract archive.
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# # Open encrypted archive
|
76
|
+
# File.open("filename.7z", "rb") do |file|
|
77
|
+
# SevenZipRuby::SevenZipReader.open(file, password: "PasswordOfArchive") do |szr|
|
78
|
+
# # Read and extract archive.
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # Open without block.
|
83
|
+
# File.open("filename.7z", "rb") do |file|
|
84
|
+
# szr = SevenZipRuby::SevenZipReader.open(file)
|
85
|
+
# # Read and extract archive.
|
86
|
+
# szr.close
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# # Open archive on memory.
|
90
|
+
# archive_data = "....."
|
91
|
+
# stream = StringIO.new(archive_data)
|
92
|
+
# SevenZipRuby::Reader.open(stream) do |szr|
|
93
|
+
# szr.extract(:all, "path_to_dir")
|
94
|
+
# end
|
95
|
+
def open(stream, param = {}, &block) # :yield: szr
|
7
96
|
szr = self.new
|
8
|
-
szr.open(
|
97
|
+
szr.open(stream, param)
|
9
98
|
if (block)
|
10
99
|
block.call(szr)
|
11
100
|
szr.close
|
@@ -14,6 +103,26 @@ module SevenZipRuby
|
|
14
103
|
end
|
15
104
|
end
|
16
105
|
|
106
|
+
# Open and extract 7zip archive.
|
107
|
+
#
|
108
|
+
# ==== Args
|
109
|
+
# +stream+ :: Input stream to read 7zip archive. <tt>stream.seek</tt> and <tt>stream.read</tt> are needed, such as <tt>File</tt> and <tt>StringIO</tt>.
|
110
|
+
# +index+ :: Index of the entry to extract. Integer or Array of Integer can be specified.
|
111
|
+
# +dir+ :: Directory to extract the archive to.
|
112
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
113
|
+
#
|
114
|
+
# ==== Examples
|
115
|
+
# File.open("filename.7z", "rb") do |file|
|
116
|
+
# SevenZipRuby::SevenZipReader.extract(file, 1, "path_to_dir")
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# File.open("filename.7z", "rb") do |file|
|
120
|
+
# SevenZipRuby::SevenZipReader.extract(file, [1, 2, 4], "path_to_dir", password: "PasswordOfArchive")
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# File.open("filename.7z", "rb") do |file|
|
124
|
+
# SevenZipRuby::SevenZipReader.extract(file, :all, "path_to_dir")
|
125
|
+
# end
|
17
126
|
def extract(stream, index, dir = ".", param = {})
|
18
127
|
password = { password: param.delete(:password) }
|
19
128
|
self.open(stream, password) do |szr|
|
@@ -21,6 +130,17 @@ module SevenZipRuby
|
|
21
130
|
end
|
22
131
|
end
|
23
132
|
|
133
|
+
# Open and extract 7zip archive.
|
134
|
+
#
|
135
|
+
# ==== Args
|
136
|
+
# +stream+ :: Input stream to read 7zip archive. <tt>stream.seek</tt> and <tt>stream.read</tt> are needed.
|
137
|
+
# +dir+ :: Directory to extract the archive to.
|
138
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
139
|
+
#
|
140
|
+
# ==== Examples
|
141
|
+
# File.open("filename.7z", "rb") do |file|
|
142
|
+
# SevenZipRuby::SevenZipReader.extract_all(file, "path_to_dir")
|
143
|
+
# end
|
24
144
|
def extract_all(stream, dir = ".", param = {})
|
25
145
|
password = { password: param.delete(:password) }
|
26
146
|
self.open(stream, password) do |szr|
|
@@ -28,14 +148,38 @@ module SevenZipRuby
|
|
28
148
|
end
|
29
149
|
end
|
30
150
|
|
31
|
-
|
32
|
-
|
151
|
+
# Open and verify 7zip archive.
|
152
|
+
#
|
153
|
+
# ==== Args
|
154
|
+
# +stream+ :: Input stream to read 7zip archive. <tt>stream.seek</tt> and <tt>stream.read</tt> are needed.
|
155
|
+
# +opt+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
156
|
+
#
|
157
|
+
# ==== Examples
|
158
|
+
# File.open("filename.7z", "rb") do |file|
|
159
|
+
# ret = SevenZipRuby::SevenZipReader.verify(file)
|
160
|
+
# # => true/false
|
161
|
+
# end
|
162
|
+
def verify(stream, opt = {})
|
163
|
+
szr = self.open(stream, opt)
|
33
164
|
ret = szr.verify
|
34
165
|
szr.close
|
35
166
|
return ret
|
36
167
|
end
|
37
168
|
end
|
38
169
|
|
170
|
+
# Open 7zip archive.
|
171
|
+
#
|
172
|
+
# ==== Args
|
173
|
+
# +stream+ :: Input stream to read 7zip archive. <tt>stream.seek</tt> and <tt>stream.read</tt> are needed.
|
174
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
175
|
+
#
|
176
|
+
# ==== Examples
|
177
|
+
# File.open("filename.7z", "rb") do |file|
|
178
|
+
# szr = SevenZipRuby::SevenZipReader.new
|
179
|
+
# szr.open(file)
|
180
|
+
# # ...
|
181
|
+
# szr.close
|
182
|
+
# end
|
39
183
|
def open(stream, param = {})
|
40
184
|
param[:password] = param[:password].to_s if (param[:password])
|
41
185
|
stream.set_encoding(Encoding::ASCII_8BIT)
|
@@ -43,61 +187,18 @@ module SevenZipRuby
|
|
43
187
|
return self
|
44
188
|
end
|
45
189
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
ret = File.open(path, "wb")
|
59
|
-
else
|
60
|
-
path = arg.path.expand_path(base_dir)
|
61
|
-
path.mkpath
|
62
|
-
set_file_attribute(path.to_s, arg.attrib) if (arg.attrib)
|
63
|
-
path.utime(arg.atime || path.atime, arg.mtime || path.mtime)
|
64
|
-
end
|
65
|
-
next ret
|
66
|
-
|
67
|
-
when :result
|
68
|
-
arg[:stream].close
|
69
|
-
unless (arg[:info].anti?)
|
70
|
-
path = arg[:info].path.expand_path(base_dir)
|
71
|
-
set_file_attribute(path.to_s, arg[:info].attrib) if (arg[:info].attrib)
|
72
|
-
path.utime(arg[:info].atime || path.atime, arg[:info].mtime || path.mtime)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
private :file_proc
|
78
|
-
|
79
|
-
def data_proc(output, idx_prj)
|
80
|
-
return Proc.new do |type, arg|
|
81
|
-
case(type)
|
82
|
-
when :stream
|
83
|
-
ret = (arg.has_data? ? StringIO.new("".b) : nil)
|
84
|
-
unless (arg.has_data?)
|
85
|
-
output[idx_prj[arg.index]] = nil
|
86
|
-
end
|
87
|
-
next ret
|
88
|
-
|
89
|
-
when :result
|
90
|
-
arg[:stream].close
|
91
|
-
if (arg[:info].has_data?)
|
92
|
-
output[idx_prj[arg[:info].index]] = arg[:stream].string
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
private :data_proc
|
99
|
-
|
100
|
-
|
190
|
+
# Verify 7zip archive.
|
191
|
+
#
|
192
|
+
# ==== Args
|
193
|
+
# none
|
194
|
+
#
|
195
|
+
# ==== Examples
|
196
|
+
# File.open("filename.7z", "rb") do |file|
|
197
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
198
|
+
# ret = szr.verify
|
199
|
+
# # => true/false
|
200
|
+
# end
|
201
|
+
# end
|
101
202
|
def test
|
102
203
|
begin
|
103
204
|
return test_all_impl(nil)
|
@@ -107,6 +208,18 @@ module SevenZipRuby
|
|
107
208
|
end
|
108
209
|
alias verify test
|
109
210
|
|
211
|
+
# Verify 7zip archive and return the result of each entry.
|
212
|
+
#
|
213
|
+
# ==== Args
|
214
|
+
# none
|
215
|
+
#
|
216
|
+
# ==== Examples
|
217
|
+
# File.open("filename.7z", "rb") do |file|
|
218
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
219
|
+
# ret = szr.verify_detail
|
220
|
+
# # => [ true, :DataError, :DataError, ... ]
|
221
|
+
# end
|
222
|
+
# end
|
110
223
|
def verify_detail
|
111
224
|
begin
|
112
225
|
return test_all_impl(true)
|
@@ -115,6 +228,24 @@ module SevenZipRuby
|
|
115
228
|
end
|
116
229
|
end
|
117
230
|
|
231
|
+
# Extract some entries of 7zip archive to local directory.
|
232
|
+
#
|
233
|
+
# ==== Args
|
234
|
+
# +index+ :: Index of the entry to extract. Integer or Array of Integer can be specified.
|
235
|
+
# +dir+ :: Directory to extract the archive to.
|
236
|
+
#
|
237
|
+
# ==== Examples
|
238
|
+
# File.open("filename.7z", "rb") do |file|
|
239
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
240
|
+
# szr.extract([ 1, 2, 4 ], "path_to_dir")
|
241
|
+
# end
|
242
|
+
# end
|
243
|
+
#
|
244
|
+
# File.open("filename.7z", "rb") do |file|
|
245
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
246
|
+
# szr.extract(:all, "path_to_dir")
|
247
|
+
# end
|
248
|
+
# end
|
118
249
|
def extract(index, dir = ".")
|
119
250
|
path = File.expand_path(dir)
|
120
251
|
case(index)
|
@@ -129,14 +260,62 @@ module SevenZipRuby
|
|
129
260
|
end
|
130
261
|
end
|
131
262
|
|
263
|
+
# Extract all entries of 7zip archive to local directory.
|
264
|
+
#
|
265
|
+
# ==== Args
|
266
|
+
# +dir+ :: Directory to extract the archive to.
|
267
|
+
#
|
268
|
+
# ==== Examples
|
269
|
+
# File.open("filename.7z", "rb") do |file|
|
270
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
271
|
+
# szr.extract_all("path_to_dir")
|
272
|
+
# end
|
273
|
+
# end
|
132
274
|
def extract_all(dir = ".")
|
133
275
|
extract_all_impl(file_proc(File.expand_path(dir)))
|
134
276
|
end
|
135
277
|
|
136
|
-
|
278
|
+
# Extract entires of 7zip archive to local directory based on the block return value.
|
279
|
+
#
|
280
|
+
# ==== Args
|
281
|
+
# +dir+ :: Directory to extract the archive to.
|
282
|
+
#
|
283
|
+
# ==== Examples
|
284
|
+
# # Extract files whose size is less than 1024.
|
285
|
+
# File.open("filename.7z", "rb") do |file|
|
286
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
287
|
+
# szr.extract_if("path_to_dir") do |entry|
|
288
|
+
# next entry.size < 1024
|
289
|
+
# end
|
290
|
+
# end
|
291
|
+
# end
|
292
|
+
def extract_if(dir = ".", &block) # :yield: entry_info
|
137
293
|
extract(entries.select(&block).map(&:index), dir)
|
138
294
|
end
|
139
295
|
|
296
|
+
# Extract some entries of 7zip archive and return the extracted data.
|
297
|
+
#
|
298
|
+
# ==== Args
|
299
|
+
# +index+ :: Index of the entry to extract. :all, Integer or Array of Integer can be specified.
|
300
|
+
#
|
301
|
+
# ==== Examples
|
302
|
+
# File.open("filename.7z", "rb") do |file|
|
303
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
304
|
+
# small_entries = szr.entries.select{ |i| i.size < 1024 }
|
305
|
+
#
|
306
|
+
# data_list = szr.extract_data(small_entries)
|
307
|
+
# # => [ "file contents1", "file contents2", ... ]
|
308
|
+
# end
|
309
|
+
# end
|
310
|
+
#
|
311
|
+
# File.open("filename.7z", "rb") do |file|
|
312
|
+
# SevenZipRuby::SevenZipReader.open(file) do |szr|
|
313
|
+
# largest_entry = szr.entries.max_by{ |i| i.file? ? i.size : 0 }
|
314
|
+
#
|
315
|
+
# data_list = szr.extract_data(largest_entry)
|
316
|
+
# # => "file contents..."
|
317
|
+
# end
|
318
|
+
# end
|
140
319
|
def extract_data(index)
|
141
320
|
case(index)
|
142
321
|
when :all
|
@@ -173,8 +352,67 @@ module SevenZipRuby
|
|
173
352
|
|
174
353
|
end
|
175
354
|
end
|
355
|
+
|
356
|
+
|
357
|
+
def file_proc(base_dir) # :nodoc:
|
358
|
+
base_dir = base_dir.to_s
|
359
|
+
return Proc.new do |type, arg|
|
360
|
+
case(type)
|
361
|
+
when :stream
|
362
|
+
ret = nil
|
363
|
+
if (arg.anti?)
|
364
|
+
arg.path.rmtree if (arg.path.exist?)
|
365
|
+
elsif (arg.file?)
|
366
|
+
path = arg.path.expand_path(base_dir)
|
367
|
+
path.parent.mkpath
|
368
|
+
ret = File.open(path, "wb")
|
369
|
+
else
|
370
|
+
path = arg.path.expand_path(base_dir)
|
371
|
+
path.mkpath
|
372
|
+
set_file_attribute(path.to_s, arg.attrib) if (arg.attrib)
|
373
|
+
path.utime(arg.atime || path.atime, arg.mtime || path.mtime)
|
374
|
+
end
|
375
|
+
next ret
|
376
|
+
|
377
|
+
when :result
|
378
|
+
arg[:stream].close
|
379
|
+
raise InvalidArchive.new("Corrupted archive or invalid password") unless (arg[:success])
|
380
|
+
|
381
|
+
unless (arg[:info].anti?)
|
382
|
+
path = arg[:info].path.expand_path(base_dir)
|
383
|
+
set_file_attribute(path.to_s, arg[:info].attrib) if (arg[:info].attrib)
|
384
|
+
path.utime(arg[:info].atime || path.atime, arg[:info].mtime || path.mtime)
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|
389
|
+
private :file_proc
|
390
|
+
|
391
|
+
def data_proc(output, idx_prj) # :nodoc:
|
392
|
+
return Proc.new do |type, arg|
|
393
|
+
case(type)
|
394
|
+
when :stream
|
395
|
+
ret = (arg.has_data? ? StringIO.new("".b) : nil)
|
396
|
+
unless (arg.has_data?)
|
397
|
+
output[idx_prj[arg.index]] = nil
|
398
|
+
end
|
399
|
+
next ret
|
400
|
+
|
401
|
+
when :result
|
402
|
+
arg[:stream].close
|
403
|
+
raise InvalidArchive.new("Corrupted archive or invalid password") unless (arg[:success])
|
404
|
+
|
405
|
+
if (arg[:info].has_data?)
|
406
|
+
output[idx_prj[arg[:info].index]] = arg[:stream].string
|
407
|
+
end
|
408
|
+
|
409
|
+
end
|
410
|
+
end
|
411
|
+
end
|
412
|
+
private :data_proc
|
176
413
|
end
|
177
414
|
|
178
415
|
|
416
|
+
# +Reader+ is an alias of +SevenZipReader+.
|
179
417
|
Reader = SevenZipReader
|
180
418
|
end
|
@@ -1,11 +1,77 @@
|
|
1
1
|
require("stringio")
|
2
2
|
|
3
3
|
module SevenZipRuby
|
4
|
+
|
5
|
+
# SevenZipWriter creates 7zip archive.
|
6
|
+
#
|
7
|
+
# == Properties
|
8
|
+
# +method+ :: Compression method. "LZMA", "LZMA2", "PPMd", "BZIP2", "DEFLATE" or "COPY". Default value is "LZMA".
|
9
|
+
# +level+ :: Compression level. 0, 1, 3, 5, 7 or 9. Default value is 5.
|
10
|
+
# +solid+ :: Solid compression. <tt>true</tt> or <tt>false</tt>. Default value is <tt>true</tt>.
|
11
|
+
# +header_compression+ :: Header compression. <tt>true</tt> or <tt>false</tt>. Default value is <tt>true</tt>.
|
12
|
+
# +header_encryption+ :: Header encryption. <tt>true</tt> or <tt>false</tt>. Default value is <tt>false</tt>.
|
13
|
+
# +multi_threading+ :: Multi threading. <tt>true</tt> or <tt>false</tt>. Default value is <tt>true</tt>.
|
14
|
+
#
|
15
|
+
# == Examples
|
16
|
+
# === Compress files
|
17
|
+
# # Compress files
|
18
|
+
# File.open("filename.7z", "wb") do |file|
|
19
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
20
|
+
# szw.add_directory("test_dir")
|
21
|
+
# szw.add_file("test.txt")
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# stream = StringIO.new("")
|
26
|
+
# SevenZipRuby::SevenZipWriter.open(stream) do |szw|
|
27
|
+
# szw.add_file("test.txt")
|
28
|
+
# szw.add_data(data, "test.bin")
|
29
|
+
# end
|
30
|
+
# # p stream.string
|
31
|
+
#
|
32
|
+
# === Set various properties
|
33
|
+
# File.open("filename.7z", "wb") do |file|
|
34
|
+
# SevenZipRuby::SevenZipWriter.open(file, password: "Password") do |szw|
|
35
|
+
# szw.method = "LZMA"
|
36
|
+
# szw.level = 9
|
37
|
+
# szw.solid = false
|
38
|
+
# szw.header_compression = false
|
39
|
+
# szw.header_encryption = true
|
40
|
+
# szw.multi_threading = false
|
41
|
+
#
|
42
|
+
# szw.add_directory("test_dir")
|
43
|
+
# end
|
44
|
+
# end
|
4
45
|
class SevenZipWriter
|
46
|
+
# Encoding used for path string in 7zip archive.
|
5
47
|
PATH_ENCODING = Encoding::UTF_8
|
6
48
|
|
7
49
|
class << self
|
8
|
-
|
50
|
+
# Open 7zip archive to write.
|
51
|
+
#
|
52
|
+
# ==== Args
|
53
|
+
# +stream+ :: Output stream to write 7zip archive. <tt>stream.write</tt> is needed.
|
54
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
55
|
+
#
|
56
|
+
# ==== Examples
|
57
|
+
# # Open archive
|
58
|
+
# File.open("filename.7z", "wb") do |file|
|
59
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
60
|
+
# # Create archive.
|
61
|
+
# # ...
|
62
|
+
# # You don't have to call szw.compress. Of cource, you may call it.
|
63
|
+
# # szw.compress
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# # Open without block.
|
68
|
+
# File.open("filename.7z", "wb") do |file|
|
69
|
+
# szw = SevenZipRuby::SevenZipWriter.open(file)
|
70
|
+
# # Create archive.
|
71
|
+
# szw.compress # Compress must be called in this case.
|
72
|
+
# szw.close
|
73
|
+
# end
|
74
|
+
def open(stream, param = {}, &block) # :yield: szw
|
9
75
|
szw = self.new
|
10
76
|
szw.open(stream, param)
|
11
77
|
if (block)
|
@@ -17,14 +83,38 @@ module SevenZipRuby
|
|
17
83
|
end
|
18
84
|
end
|
19
85
|
|
86
|
+
# Create 7zip archive which includes the specified directory recursively.
|
87
|
+
#
|
88
|
+
# ==== Args
|
89
|
+
# +stream+ :: Output stream to write 7zip archive. <tt>stream.write</tt> is needed.
|
90
|
+
# +dir+ :: Directory to be added to the 7zip archive. <b><tt>dir</tt></b> must be a <b>relative path</b>.
|
91
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
92
|
+
#
|
93
|
+
# ==== Examples
|
94
|
+
# # Create 7zip archive which includes 'dir'.
|
95
|
+
# File.open("filename.7z", "wb") do |file|
|
96
|
+
# SevenZipRuby::SevenZipWriter.add_directory(file, 'dir')
|
97
|
+
# end
|
20
98
|
def add_directory(stream, dir, param = {})
|
21
99
|
password = { password: param.delete(:password) }
|
22
100
|
self.open(stream, password) do |szw|
|
23
101
|
szw.add_directory(dir, param)
|
24
102
|
end
|
25
103
|
end
|
26
|
-
alias add_dir add_directory
|
104
|
+
alias add_dir add_directory # +add_dir+ is an alias of +add_directory+.
|
27
105
|
|
106
|
+
# Create 7zip archive which includes the specified file recursively.
|
107
|
+
#
|
108
|
+
# ==== Args
|
109
|
+
# +stream+ :: Output stream to write 7zip archive. <tt>stream.write</tt> is needed.
|
110
|
+
# +file+ :: File to be added to the 7zip archive. <b><tt>file</tt></b> must be a <b>relative path</b>.
|
111
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
112
|
+
#
|
113
|
+
# ==== Examples
|
114
|
+
# # Create 7zip archive which includes 'file.txt'.
|
115
|
+
# File.open("filename.7z", "wb") do |file|
|
116
|
+
# SevenZipRuby::SevenZipWriter.add_file(file, 'file.txt')
|
117
|
+
# end
|
28
118
|
def add_file(stream, filename, param = {})
|
29
119
|
password = { password: param.delete(:password) }
|
30
120
|
self.open(stream, password) do |szw|
|
@@ -33,17 +123,69 @@ module SevenZipRuby
|
|
33
123
|
end
|
34
124
|
end
|
35
125
|
|
126
|
+
# Open 7zip archive to create.
|
127
|
+
#
|
128
|
+
# ==== Args
|
129
|
+
# +stream+ :: Output stream to write 7zip archive. <tt>stream.write</tt> is needed.
|
130
|
+
# +param+ :: Optional hash parameter. <tt>:password</tt> key represents password of this archive.
|
131
|
+
#
|
132
|
+
# ==== Examples
|
133
|
+
# File.open("filename.7z", "wb") do |file|
|
134
|
+
# szw = SevenZipRuby::SevenZipWriter.open(file)
|
135
|
+
# # ...
|
136
|
+
# szw.compress
|
137
|
+
# szw.close
|
138
|
+
# end
|
36
139
|
def open(stream, param = {})
|
37
140
|
stream.set_encoding(Encoding::ASCII_8BIT)
|
38
141
|
open_impl(stream, param)
|
39
142
|
return self
|
40
143
|
end
|
41
144
|
|
145
|
+
# Compress and output data to archive file.
|
146
|
+
# You don't have to call this method when you use block-style SevenZipWriter.open.
|
147
|
+
#
|
148
|
+
# ==== Examples
|
149
|
+
# # Open archive
|
150
|
+
# File.open("filename.7z", "wb") do |file|
|
151
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
152
|
+
# # Create archive.
|
153
|
+
# # ...
|
154
|
+
# # You don't have to call szw.compress. Of cource, you may call it.
|
155
|
+
# # szw.compress
|
156
|
+
# end
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# # Open without block.
|
160
|
+
# File.open("filename.7z", "wb") do |file|
|
161
|
+
# szw = SevenZipRuby::SevenZipWriter.open(file)
|
162
|
+
# # Create archive.
|
163
|
+
# szw.compress # Compress must be called in this case.
|
164
|
+
# szw.close
|
165
|
+
# end
|
42
166
|
def compress
|
43
167
|
compress_impl(compress_proc)
|
44
168
|
return self
|
45
169
|
end
|
46
170
|
|
171
|
+
# Add file entry to 7zip archive.
|
172
|
+
#
|
173
|
+
# ==== Args
|
174
|
+
# +filename+ :: File to be added to the 7zip archive. <tt>file</tt> must be a <b>relative path</b> if <tt>:as</tt> option is not specified.
|
175
|
+
# +opt+ :: Optional hash parameter. <tt>:as</tt> key represents filename used in this archive.
|
176
|
+
#
|
177
|
+
# ==== Examples
|
178
|
+
# File.open("filename.7z", "wb") do |file|
|
179
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
180
|
+
# # Add file entry 'test.txt' in 7zip archive.
|
181
|
+
# # This entry has the contents of the local file 'test.txt'.
|
182
|
+
# szw.add_file("test.txt")
|
183
|
+
#
|
184
|
+
# # Add file entry 'desk/test.txt' in 7zip archive.
|
185
|
+
# # This entry has the contents of the local file 'C:/Users/test/Desktop/test2.txt'.
|
186
|
+
# szw.add_file("C:/Users/test/Desktop/test2.txt", as: "desk/test.txt")
|
187
|
+
# end
|
188
|
+
# end
|
47
189
|
def add_file(filename, opt={})
|
48
190
|
path = Pathname(filename)
|
49
191
|
check_option(opt, [ :as ])
|
@@ -60,6 +202,23 @@ module SevenZipRuby
|
|
60
202
|
return self
|
61
203
|
end
|
62
204
|
|
205
|
+
# Add file entry to 7zip archive.
|
206
|
+
#
|
207
|
+
# ==== Args
|
208
|
+
# +data+ :: Data to be added to the 7zip archive.
|
209
|
+
# +filename+ :: File name of the entry to be added to the 7zip archive. <tt>filename</tt> must be a <b>relative path</b>.
|
210
|
+
# +opt+ :: Optional hash parameter. <tt>:ctime</tt>, <tt>:atime</tt> and <tt>:mtime</tt> keys can be specified as timestamp.
|
211
|
+
#
|
212
|
+
# ==== Examples
|
213
|
+
# File.open("filename.7z", "wb") do |file|
|
214
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
215
|
+
# data = "1234567890"
|
216
|
+
#
|
217
|
+
# # Add file entry 'data.bin' in 7zip archive.
|
218
|
+
# # This entry has the contents "1234567890".
|
219
|
+
# szw.add_data(data, "data.bin")
|
220
|
+
# end
|
221
|
+
# end
|
63
222
|
def add_data(data, filename, opt={})
|
64
223
|
path = Pathname(filename)
|
65
224
|
raise ArgumentError.new("filename should be relative") if (path.absolute?)
|
@@ -70,6 +229,22 @@ module SevenZipRuby
|
|
70
229
|
return self
|
71
230
|
end
|
72
231
|
|
232
|
+
# Add directory and files recursively to 7zip archive.
|
233
|
+
#
|
234
|
+
# ==== Args
|
235
|
+
# +directory+ :: Directory to be added to the 7zip archive. <tt>directory</tt> must be a <b>relative path</b> if <tt>:as</tt> option is not specified.
|
236
|
+
# +opt+ :: Optional hash parameter. <tt>:as</tt> key represents directory name used in this archive.
|
237
|
+
#
|
238
|
+
# ==== Examples
|
239
|
+
# File.open("filename.7z", "wb") do |file|
|
240
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
241
|
+
# # Add "dir1" and entries under "dir" recursively.
|
242
|
+
# szw.add_directory("dir1")
|
243
|
+
#
|
244
|
+
# # Add "C:/Users/test/Desktop/dir" and entries under it recursively.
|
245
|
+
# szw.add_directory("C:/Users/test/Desktop/dir", as: "test/dir")
|
246
|
+
# end
|
247
|
+
# end
|
73
248
|
def add_directory(directory, opt={})
|
74
249
|
directory = Pathname(directory).cleanpath
|
75
250
|
check_option(opt, [ :as ])
|
@@ -100,8 +275,21 @@ module SevenZipRuby
|
|
100
275
|
|
101
276
|
return self
|
102
277
|
end
|
103
|
-
alias add_dir add_directory
|
278
|
+
alias add_dir add_directory # +add_dir+ is an alias of +add_directory+.
|
104
279
|
|
280
|
+
# Add an entry of empty directory to 7zip archive.
|
281
|
+
#
|
282
|
+
# ==== Args
|
283
|
+
# +directory_name+ :: Directory name to be added to 7z archive.
|
284
|
+
# +opt+ :: Optional hash parameter. <tt>:ctime</tt>, <tt>:atime</tt> and <tt>:mtime</tt> keys can be specified as timestamp.
|
285
|
+
#
|
286
|
+
# ==== Examples
|
287
|
+
# File.open("filename.7z", "wb") do |file|
|
288
|
+
# SevenZipRuby::SevenZipWriter.open(file) do |szw|
|
289
|
+
# # Add an empty directory "dir1".
|
290
|
+
# szw.mkdir("dir1")
|
291
|
+
# end
|
292
|
+
# end
|
105
293
|
def mkdir(directory_name, opt={})
|
106
294
|
path = Pathname(directory_name)
|
107
295
|
raise ArgumentError.new("directory_name should be relative") if (path.absolute?)
|
@@ -113,13 +301,13 @@ module SevenZipRuby
|
|
113
301
|
end
|
114
302
|
|
115
303
|
|
116
|
-
def check_option(opt, keys)
|
304
|
+
def check_option(opt, keys) # :nodoc:
|
117
305
|
invalid_keys = opt.keys - keys
|
118
306
|
raise ArgumentError.new("invalid option: " + invalid_keys.join(", ")) unless (invalid_keys.empty?)
|
119
307
|
end
|
308
|
+
private :check_option
|
120
309
|
|
121
|
-
|
122
|
-
def compress_proc
|
310
|
+
def compress_proc # :nodoc:
|
123
311
|
return Proc.new do |type, info|
|
124
312
|
case(type)
|
125
313
|
when :stream
|
@@ -139,5 +327,6 @@ module SevenZipRuby
|
|
139
327
|
end
|
140
328
|
|
141
329
|
|
330
|
+
# +Writer+ is an alias of +SevenZipWriter+.
|
142
331
|
Writer = SevenZipWriter
|
143
332
|
end
|
data/spec/seven_zip_ruby_spec.rb
CHANGED
@@ -158,6 +158,16 @@ describe SevenZipRuby do
|
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
161
|
+
example "invalid password" do
|
162
|
+
File.open(SevenZipRubySpecHelper::SEVEN_ZIP_PASSWORD_FILE, "rb") do |file|
|
163
|
+
expect{ SevenZipRuby::Reader.open(file){ |szr| szr.extract_data(1) } }.to raise_error
|
164
|
+
end
|
165
|
+
|
166
|
+
File.open(SevenZipRubySpecHelper::SEVEN_ZIP_PASSWORD_FILE, "rb") do |file|
|
167
|
+
expect{ SevenZipRuby::Reader.open(file, password: "a"){ |szr| szr.extract_data(1) } }.to raise_error
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
161
171
|
example "raise error in open" do
|
162
172
|
error = StandardError.new
|
163
173
|
|
@@ -265,6 +275,7 @@ describe SevenZipRuby do
|
|
265
275
|
szw.add_data("This is hoge.txt content.", "hoge.txt")
|
266
276
|
szw.add_data("This is hoge2.txt content.", "hoge2.txt")
|
267
277
|
szw.mkdir("hoge/hoge/hoge")
|
278
|
+
szw.compress
|
268
279
|
end
|
269
280
|
end
|
270
281
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seven_zip_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: x64-mingw32
|
6
6
|
authors:
|
7
7
|
- Masamitsu MURASE
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|