forked from borisbrodski/sevenzipjbinding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextraction_snippets.html
334 lines (273 loc) · 12.6 KB
/
extraction_snippets.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<h1>Extraction code snippets</h1>
<h2>Index</h2>
<ul>
<li><a href="#extracting">Extracting & browsing existing archives</a></li>
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#std-vs-simple">Standard vs Simple interface</a></li>
<li><a href="#quering-archive">Quering items in archive</a>
<ul>
<li><a href="#quering-archive-simple-int">Simple interface</a></li>
<li><a href="#quering-archive-std-int">Standard interface</a></li>
</ul>
</li>
<li><a href="#extracting-single-file"> Extracting of a single
file</a>
<ul>
<li><a href="#extracting-single-file-simple-int">Simple
interface</a></li>
<li><a href="#extracting-single-file-std-int">Standard
interface</a>
<ul>
<li><a href="#extracting-archive-std-int-callback">
Extracting archive using standard interface and call back method as a
filter</a></li>
<li><a href="#extracting-archive-std-int-prepared-list">
Extracting archive using standard interface and prepared list of
items to extract</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#open-multipart-archives">Open multi-part
archives</a>
<ul>
<li><a href="#open-multipart-7z-archives">Open multi-part 7z
archives</a></li>
<li><a href="#open-multipart-rar-archives">Open multi-part
RAR archives</a></li>
</ul>
</li>
</ul>
</ul>
<h1><a name="extracting">Extracting & browsing existing archives</h1>
<h2><a name="overview">Overview</a></h2>
<p>Here you can find many code snippets, example of how to
perform basic operations with 7-Zip-JBinding. All examples are part of the JUnit tests.
Each example runs on one of the test archives. JUnit tests check, that the output of the
snippet is exactly the one published on this page. Below is a list of the test archives
for this page. You can download all test archives in as a
<a href="test-archives/7-Zip-JBinding-all-test-archives-for-snippets.zip">single ZIP file</a>.
</p>
<ul>
<li><a href="test-archives/simple.zip"><span class="filename">simple.zip</span></a>
<div class="fragment"><pre class="fragment">
$ 7z l simple.zip
7-Zip 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
p7zip Version 4.65 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)
Listing archive: simple.zip
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2009-08-04 11:26:16 ..... 4481 68 file1.txt
2009-08-04 11:27:34 ..... 75 62 file2.txt
2009-08-04 13:53:06 D.... 0 0 folder
2009-08-04 13:53:40 ..... 6 6 folder/file in folder.txt
------------------- ----- ------------ ------------ ------------------------
4562 136 3 files, 1 folders
</pre></div>
</li>
<li>
<a href="test-archives/multipart-7z.7z.001"><span class="filename">multipart-7z.7z.001</span></a>;
<a href="test-archives/multipart-7z.7z.002"><span class="filename">multipart-7z.7z.002</span></a>;
<a href="test-archives/multipart-7z.7z.003"><span class="filename">multipart-7z.7z.003</span></a>;
<a href="test-archives/multipart-7z.7z.004"><span class="filename">multipart-7z.7z.004</span></a>
<div class="fragment"><pre class="fragment">
$ 7z l multipart-7z.7z.001
7-Zip 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
p7zip Version 4.65 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)
Listing archive: multipart-7z.7z.001
Method = LZMA
Solid = +
Blocks = 1
Physical Size = 331
Headers Size = 217
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2009-08-04 13:53:39 ....A 6 114 folder/file in folder.txt
2009-08-04 11:26:16 ....A 4481 file1.txt
2009-08-04 11:27:33 ....A 75 file2.txt
2009-08-04 13:53:05 D.... 0 0 folder
------------------- ----- ------------ ------------ ------------------------
4562 114 3 files, 1 folders
</pre></div>
</li>
<li>
<a href="test-archives/multipart-rar.part1.rar"><span class="filename">multipart-rar.part1.rar</span></a>;
<a href="test-archives/multipart-rar.part2.rar"><span class="filename">multipart-rar.part2.rar</span></a>;
<a href="test-archives/multipart-rar.part3.rar"><span class="filename">multipart-rar.part3.rar</span></a>;
<a href="test-archives/multipart-rar.part4.rar"><span class="filename">multipart-rar.part4.rar</span></a>;
<a href="test-archives/multipart-rar.part5.rar"><span class="filename">multipart-rar.part5.rar</span></a>
<div class="fragment"><pre class="fragment">
$ 7z l multipart-rar.part1.rar
7-Zip 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
p7zip Version 4.65 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)
Listing archive: multipart-rar.part1.rar
Solid = -
Blocks = 4
Multivolume = +
Volumes = 5
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2009-08-04 13:53:39 ..... 6 6 folder/file in folder.txt
2009-08-04 11:26:16 ..... 4481 4481 file1.txt
2009-08-04 11:27:33 ..... 75 75 file2.txt
2009-08-04 13:53:05 D.... 0 0 folder
------------------- ----- ------------ ------------ ------------------------
4562 4562 3 files, 1 folders
</pre></div>
</li>
</ul>
<br />
<h2><a name="std-vs-simple">Standard vs Simple interface</a></h2>
<p>The 7-Zip-JBinding library offers two different interfaces:
standard and simple. The standard interface directly maps all native
7-Zip library methods providing C++ like interface. The simple interface
is a try to provide more Java-like easy to use interface. It was
designed either for quick coding and easy start and not for feature
richness nor for performance.</p>
<p>The following examples will be presented in both standard and
simple interface beginning with as simple interface. Please note, that
the full performance can only be reached with the standard interface.</p>
<br />
<h2><a name="quering-archive">Quering items in archive</a></h2>
<h3><a name="quering-archive-simple-int">Simple interface</a></h3>
<p>This example shows how to print a list of items of archive using
simple interface.</p>
##INCLUDE_SNIPPET(ListItemsSimple)
<p>The output for the test archive:</p>
##INCLUDE_OUTPUT(ListItemsOutput)
<p>Please note, that not all archive formats support the "folder"
items, like "folder/" in our example. Also please note, that packed size
in this example as almost all other properties can return null. A
returned null value means, that either the property is not supported by
the particular archive format or not known for the particular item.</p>
<h3><a name="quering-archive-std-int">Standard interface</a></h3>
<p>This example shows how to print a list of items of archive using
standard interface.</p>
##INCLUDE_SNIPPET(ListItemsStandard)
<p>The output for the test archive is the same:</p>
##INCLUDE_OUTPUT(ListItemsOutput)
<h2><a name="extracting-single-file">Extracting of a single
file</a></h2>
<p>This section will show how to access the compressed data of the
archive items. The following examples will calculate a simple checksum
of each archive item.</p>
<h3><a name="extracting-single-file-simple-int">Simple
interface</a></h3>
<p>This example shows how to get content of the items of the archive
using simple interface. In order to consume extracted content the simple
checksum will be calculated.</p>
##INCLUDE_SNIPPET(ExtractItemsSimple)
<p>Here is the output of the last snippet:</p>
##INCLUDE_OUTPUT(ExtractItems)
<h3><a name="extracting-single-file-std-int">Standard interface</a></h3>
<p>The following two examples show how to perform the same
extracting task using standard interface. As in the previous example,
the simple checksum will be calculated in order to consume the extracted
data.</p>
<p>First example traverse the entire archive and uses call back
method to choose on the fly whether proceed with the extracting of an
item or not. The second example prepare a list of items for extracting.
</p>
<h4><a name="extracting-archive-std-int-callback"> Extracting
archive using standard interface and call back method as a filter</a></h4>
##INCLUDE_SNIPPET(ExtractItemsStandardCallback)
<h4><a name="extracting-archive-std-int-prepared-list">
Extracting archive using standard interface and prepared list of items
to extract</a></h4>
##INCLUDE_SNIPPET(ExtractItemsStandard)
<h2><a name="open-multipart-archives">Open multi-part archives</a></h2>
<p>Dealing with a multi-part archives is not so easy, as dealing
with a ordinary single part archives. Different archive formats need
different approaches and therefore will be discussed separately.</p>
<h3><a name="open-multipart-7z-archives">Open multi-part 7z
archives</a></h3>
<p>Multi-part 7z archives are build by simple splitting of solid 7z
archives into multiple peaces. This means, you can easy convert a
multi-part 7z archive into single part 7z archive by concatenation of
all archive parts into a one single file. Here is an example, how you
can do this in a sh-shell.</p>
<div class="fragment"><pre class="fragment">
$ ###
$ ### <span class="java-comment">Compress some files into 7z archive.</span>
$ ### <span class="java-comment">Create mult-part archive 10.240 bytes for each part.</span>
$ ###
$ 7z a test.7z -v10k somedir/
...
$ ls -l
total 32
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris 6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### <span class="java-comment">Concatenate all parts into a single file</span>
$ ###
$ cat test.7z.* > test.7z
$ ls -l
total 60
-rw-r--r-- 1 boris boris <b>27164</b> 2009-11-29 01:18 <b>test.7z</b>
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris 6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### <span class="java-comment">Test archive</span>
$ ###
$ 7z t test.7z
...
Everything is Ok
...
###
### <span class="java-comment">Split solid archive back into peaces</span>
### <span class="java-comment">Split starts counting from 000, so rename files</span>
###
$ split -b 15000 -a 3 -d test.7z test2.7z.
$ mv test2.7z.001 test2.7z.002
$ mv test2.7z.000 test2.7z.001
$ ls -l
total 88
-rw-r--r-- 1 boris boris <b>15000</b> 2009-11-29 01:36 <b>test2.7z.001</b>
-rw-r--r-- 1 boris boris <b>12164</b> 2009-11-29 01:36 <b>test2.7z.002</b>
-rw-r--r-- 1 boris boris 27164 2009-11-29 01:22 test.7z
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris 6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### <span class="java-comment">Test new multi-part archive file archive</span>
$ ###
$ 7z t test2.7z.001
...
Everything is Ok
...
</pre></div>
<p>The naming conventions for the volumes of a multi-part 7z archive
are:</p>
<ul>
<li>Every file name of a volume should ends with ".7z.XXX", where
X is a digit between 0 and 9. The XXX is a decimal sequential number of
the volume starting with 1. Ending ".7z.000" is not allowed.</li>
</ul>
<p>It's important to provide corrent names in
IArchiveOpenVolumeCallback.getProperty() method and to
VolumedArchiveInStream class. Also you will probably need to parse
filenames passed to IArchiveOpenVolumeCallback.getStream(String
filename) in order to get a number of the required volume.</p>
<p>Here is an example of how to open a multi-part archive directly
from the file system. In this example all the volumes are named
correctly, so it's no need to parse requred volume name to extract the
index. The volumes already exists under the passed names und can be
directly opened.</p>
##INCLUDE_SNIPPET(OpenMultipartArchive7z)
<p>Here is the output of the last snippet:</p>
##INCLUDE_OUTPUT(OpenMultipart7zArchiveOutput)
<h3><a name="open-multipart-rar-archives">Open multi-part RAR
archives</a></h3>
<p>Since RAR uses its own proprietary format for splitting archives into
multiple volumes, there is no need to use VolumedArchiveInStream in
order to open multi-part RAR archives. To provide access to the different
volumes of a RAR archive, the archive open callback class should
additionally implement the IArchiveOpenVolumeCallback interface.
Here is an example:
</p>
##INCLUDE_SNIPPET(OpenMultipartArchiveRar)
<p>Here is the output of the last snippet:</p>
##INCLUDE_OUTPUT(OpenMultipartRarArchiveOutput)