Debian Codegraph @

www/zipfile.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The SQLite Zipfile Module</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The SQLite Zipfile Module
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#obtaining_and_compiling_zipfile">2. Obtaining and Compiling Zipfile</a></div>
130 <div class="fancy-toc1"><a href="#using_zipfile">3. Using Zipfile</a></div>
131 <div class="fancy-toc2"><a href="#table_valued_function_read_only_access_">3.1. Table-Valued Function (read-only access)</a></div>
132 <div class="fancy-toc2"><a href="#virtual_table_interface_read_write_access_">3.2. Virtual Table Interface (read/write access)</a></div>
133 <div class="fancy-toc3"><a href="#adding_entries_to_a_zip_archive">3.2.1. Adding Entries to a Zip Archive</a></div>
134 <div class="fancy-toc3"><a href="#_deleting_zip_archive_entries_">3.2.2. Deleting Zip Archive Entries </a></div>
135 <div class="fancy-toc3"><a href="#_updating_existing_zip_archive_entries_">3.2.3. Updating Existing Zip Archive Entries </a></div>
136 <div class="fancy-toc2"><a href="#_the_zipfile_aggregate_function_">3.3. The zipfile() Aggregate Function </a></div>
137 </div>
138 </div>
139 <script>
140 function toggle_toc(){
141 var sub = document.getElementById("toc_sub")
142 var mk = document.getElementById("toc_mk")
143 if( sub.style.display!="block" ){
144 sub.style.display = "block";
145 mk.innerHTML = "&#x25bc;";
146 } else {
147 sub.style.display = "none";
148 mk.innerHTML = "&#x25ba;";
149 }
150 }
151 </script>
152 </div>
153
154
155
156
157 <h1 id="overview"><span>1. </span>Overview</h1>
158
159 <p> The zipfile module provides read/write access to simple
160 <a href="https://en.wikipedia.org/wiki/Zip_%28file_format%29">ZIP archives</a>.
161 The current implementation has the following restrictions:
162
163 </p><ul>
164 <li> Does not support encryption.
165 </li><li> Does not support ZIP archives that span multiple files.
166 </li><li> Does not support zip64 extensions.
167 </li><li> The only compression algorithm supported is
168 <a href="https://zlib.net">"deflate"</a>.
169 </li></ul>
170
171 <p> Some or all of these restrictions may be removed in the future.
172
173 </p><h1 id="obtaining_and_compiling_zipfile"><span>2. </span>Obtaining and Compiling Zipfile</h1>
174
175 <p>The code for the zipfile module is found in the
176 <a href="https://sqlite.org/src/file/ext/misc/zipfile.c">ext/misc/zipfile.c</a>
177 file of the
178 <a href="https://sqlite.org/src">main SQLite source tree</a>.
179 It may be compiled into an SQLite
180 <a href="loadext.html">loadable extension</a> using a command like:
181
182 </p><div class="codeblock"><pre>gcc -g -fPIC -shared zipfile.c -o zipfile.so
183 </pre></div>
184
185 <p>Alternatively, the zipfile.c file may be compiled into the application.
186 In this case, the following function should be invoked to register the
187 extension with each new database connection:
188
189 </p><div class="codeblock"><pre>int sqlite3_zipfile_init(sqlite3 *db, void*, void*);
190 </pre></div>
191
192 <p> The first argument passed should be the database handle to register the
193 extension with. The second and third arguments should both be passed 0.
194
195 </p><p> Zipfile is included in most builds of the <a href="cli.html">command-line shell</a>.
196
197 </p><h1 id="using_zipfile"><span>3. </span>Using Zipfile</h1>
198
199 <p>The zipfile module provides three similar interfaces for accessing, updating
200 and creating zip file archives:
201
202 </p><ol>
203 <li> A table-valued function, which provides read-only access to existing
204 archives, either from the file-system or in-memory.
205 </li><li> A virtual table, which provides read and write access to archives
206 stored in the file-system.
207 </li><li> An SQL aggregate function, which can be used to create new archives
208 in memory.
209 </li></ol>
210
211 <p>The zipfile module provides two similar interfaces for accessing zip
212 archives. A table-valued function, which provides read-only access to
213 existing archives, and a virtual table interface, which provides both
214 read and write access.
215
216 </p><h2 id="table_valued_function_read_only_access_"><span>3.1. </span>Table-Valued Function (read-only access)</h2>
217
218 <p>For reading existing zip archives, the Zipfile module provides a
219 <a href="vtab.html#tabfunc2">table-valued function</a> that accepts a single argument. If the argument
220 is a text value, then it is a path to a zip archive to read from the
221 file-system. Or, if the argument is an SQL blob, then it is the zip
222 archive data itself.
223
224 </p><p>For example, to inspect the contents of zip archive "test.zip" from
225 the current directory:
226
227 </p><div class="codeblock"><pre>SELECT * FROM zipfile('test.zip');
228 </pre></div>
229
230 <p>Or, from the SQLite shell tool (the <a href="cli.html#fileio">readfile()</a>
231 function reads the contents of a file from the file-system and returns it as a
232 blob):
233
234 </p><div class="codeblock"><pre>SELECT * FROM zipfile( readfile('test.zip') );
235 </pre></div>
236
237 <p>The table-valued function returns one row for each record (file,
238 directory or symbolic link) in the zip archive. Each row has the
239 following columns:
240
241 </p><table striped="1" style="margin:1em auto; width:80%; border-spacing:0">
242 <tr style="text-align:left"><th>Column&nbsp;Name</th><th>Contents
243 </th></tr><tr style="text-align:left;background-color:#DDDDDD"><td>name </td><td> File name/path for the zip file record.
244 </td></tr><tr style="text-align:left"><td>mode </td><td> UNIX mode, as returned by stat(2) for the zip file record (an
245 integer). This identifies the type of record (file, directory
246 or symbolic link), and the associated user/group/all
247 permissions.
248 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>mtime </td><td> UTC timestamp, in seconds since the UNIX epoch (an integer).
249 </td></tr><tr style="text-align:left"><td>sz </td><td> Size of associated data in bytes after it has been
250 uncompressed (an integer).
251 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>rawdata </td><td> Raw (possibly compressed) data associated with zip file
252 entry (a blob).
253 </td></tr><tr style="text-align:left"><td>data </td><td> If the compression method for the record is either 0 or 8
254 (see below), then the uncompressed data associated with the
255 zip file entry. Or, if the compression method is not 0 or 8,
256 this column contains a NULL value.
257 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>method </td><td> The compression method used to compress the data (an
258 integer). The value 0 indicates that the data is stored
259 in the zip archive without compression. 8 means the
260 raw deflate algorithm.
261 </td></tr></table>
262
263 <h2 id="virtual_table_interface_read_write_access_"><span>3.2. </span>Virtual Table Interface (read/write access)</h2>
264
265 <p>In order to create or modify an existing zip file, a "zipfile" virtual
266 table must be created in the database schema. The CREATE VIRTUAL TABLE
267 statement expects a path to the zip file as its only argument. For example, to
268 write to zip file "test.zip" in the current directory, a zipfile table may be
269 created using:
270
271 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip');
272 </pre></div>
273
274 <p>Such a virtual table has the same columns as the table-valued function
275 described in the previous section. It may be read from using a SELECT
276 statement in the same way as the table-valued function can.
277
278 </p><p>Using the virtual table interface, new entries may be added to a zip
279 archive by inserting new rows into the virtual table. Entries may be
280 removed by deleting rows or modified by updating them.
281
282 </p><a name="adding_entries_to_a_zip_archive"></a>
283 <h3 tags="Adding to Zip" id="adding_entries_to_a_zip_archive"><span>3.2.1. </span>Adding Entries to a Zip Archive</h3>
284
285 <p>Entries may be added to a zip archive by inserting new rows. The easiest
286 way to do this is to specify values for the "name" and "data" columns only and
287 have zipfile fill in sensible defaults for other fields. To insert a directory
288 into the archive, set the "data" column to NULL. For example, to add the
289 directory "dir1" and the file "m.txt" containing the text "abcdefghi" to zip
290 archive "test.zip":
291
292 </p><div class="codeblock"><pre>INSERT INTO temp.zip(name, data) VALUES('dir1', NULL); <i>-- Add directory </i>
293 INSERT INTO temp.zip(name, data) VALUES('m.txt', 'abcdefghi'); <i>-- Add regular file </i>
294 </pre></div>
295
296 <p>When a directory is inserted, if the "name" value does not end with
297 a '/' character, the zipfile module appends one. This is necessary for
298 compatibility with other programs (most notably "info-zip") that
299 manipulate zip archives.
300
301 </p><p>To insert a symbolic link, the user must also supply a "mode" value.
302 For example, to add a symbolic link from "link.txt" to "m.txt":
303
304 </p><div class="codeblock"><pre>INSERT INTO temp.zip(name, mode, data) VALUES('link.txt', 'lrwxrw-rw-', 'm.txt');
305 </pre></div>
306
307 <p>The following rules and caveats apply to the values specified as part of
308 each INSERT statement:
309
310 </p><table striped="1" style="margin:1em auto; width:80%; border-spacing:0">
311 <tr style="text-align:left"><th>Columns </th><th> Notes
312 </th></tr><tr style="text-align:left;background-color:#DDDDDD"><td> name
313 </td><td> A non-NULL text value must be specified for the name column.
314 It is an error if the specified name already exists in the
315 archive.
316
317 </td></tr><tr style="text-align:left"><td> mode
318 </td><td> If NULL is inserted into the mode column, then the mode of the
319 new archive entry is automatically set to either 33188 (-rw-r--r--)
320 or 16877 (drwxr-xr-x), depending on whether or not the values
321 specified for columns "sz", "data" and "rawdata" indicate that
322 the new entry is a directory.<br><br>
323
324 If the specified value is an integer (or text that looks like
325 an integer), it is inserted verbatim. If the value is not a valid UNIX
326 mode, some programs may behave unexpectedly when extracting files
327 from the archive.<br><br>
328
329 Finally, if the value specified for this column is not an integer
330 or a NULL, then it is assumed to be a UNIX permissions string similar
331 to those output by the "ls -l" command (e.g. "-rw-r--r--", "drwxr-xr-x"
332 etc.). In this case, if the string cannot be parsed it is an error.
333
334 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td> mtime
335 </td><td> If NULL is inserted into the mtime column, then the timestamp
336 of the new entry is set to the current time. Otherwise, the specified
337 value is interpreted as an integer and used as is.
338
339 </td></tr><tr style="text-align:left"><td> sz
340 </td><td> This column must be set to NULL. If a non-NULL value is inserted into
341 this column, or if a new non-NULL value is provided using an UPDATE
342 statement, it is an error.
343
344 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td> rawdata
345 </td><td> This column must be set to NULL. If a non-NULL value is inserted into
346 this column, or if a new non-NULL value is provided using an UPDATE
347 statement, it is an error.
348
349 </td></tr><tr style="text-align:left"><td> data
350 </td><td>
351 To insert a directory into the archive, this field must be set to
352 NULL. In this case if a value was explicitly specified for the "mode"
353 column, then it must be consistent with a directory (i.e. it must be
354 true that (mode & 0040000)=0040000). <br><br>
355
356 Otherwise, the value inserted into this field is the file contents
357 for a regular file, or the target of a symbolic link.
358 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td> method
359 </td><td>
360 This field must be set one of integer values 0 and 8, or else to
361 NULL. <br><br>
362
363 For a directory entry, any value inserted into this field is ignored.
364 Otherwise, if it is set to 0, then the file data or symbolic link
365 target is stored as is in the zip archive and the compression method
366 set to 0. If it is set to 8, then the file data or link target is
367 compressed using deflate compression before it is stored and the
368 compression method set to 8. Finally, if a NULL value is written
369 to this field, the zipfile module automatically decides whether
370 or not to compress the data before storing it.
371 </td></tr></table>
372
373 <p> Specifying an explicit value for the rowid field as part of an INSERT
374 statement is not supported. Any value supplied is ignored.
375
376 </p><h3 id="_deleting_zip_archive_entries_"><span>3.2.2. </span> Deleting Zip Archive Entries </h3>
377
378 <p>Records may be removed from an existing zip archive by deleting the
379 corresponding rows. For example, to remove file "m.txt" from zip archive
380 "test.zip" using the virtual table created above:
381
382 </p><div class="codeblock"><pre>DELETE FROM temp.zip WHERE name = 'm.txt';
383 </pre></div>
384
385 <p>Note that deleting records from a zip archive does not reclaim the
386 space used within the archive - it merely removes an entry from the
387 archives "Central Directory Structure", making the entry inaccessible.
388 One way to work around this inefficiency is to create a new zip
389 archive based on the contents of the edited archive. For example, after
390 editing the archive accessed via virtual table temp.zzz:
391
392 </p><div class="codeblock"><pre><i>-- Create a new, empty, archive: </i>
393 CREATE VIRTUAL TABLE temp.newzip USING zipfile('new.zip');
394
395 <i>-- Copy the contents of the existing archive into the new archive</i>
396 INSERT INTO temp.newzip(name, mode, mtime, data, method)
397 SELECT name, mode, mtime, data, method FROM temp.zzz;
398 </pre></div>
399
400 <h3 id="_updating_existing_zip_archive_entries_"><span>3.2.3. </span> Updating Existing Zip Archive Entries </h3>
401
402 <p>Existing zip archive entries may be modified using UPDATE statements.
403
404 </p><p>The three leftmost columns of a zipfile virtual table, "name", "mode"
405 and "mtime", may each be set to any value that may be inserted into the same
406 column (see above). If either "mode" or "mtime" is set to NULL, the final
407 value is determined as described for an INSERT of a NULL value - the current
408 time for "mtime" and either 33188 or 16877 for "mode", depending on whether
409 or not the values specified for the next four columns of the zipfile table
410 indicate that the entry is a directory or a file.
411
412 </p><p>It is an error to attempt to set the sz or rawdata field to any value
413 other than NULL.
414
415 </p><p>The data and method columns may also be set as described for an INSERT
416 above.
417
418 </p><h2 id="_the_zipfile_aggregate_function_"><span>3.3. </span> The zipfile() Aggregate Function </h2>
419
420 <p> New zip archives may be constructed entirely within memory using the
421 zipfile() aggregate function. Each row visited by the aggregate function
422 adds an entry to the zip archive. The value returned is a blob containing
423 the entire archive image.
424
425 </p><p> The zipfile() aggregate function may be called with 2, 4 or 5
426 arguments. If it is called with 5 arguments, then the entry added to
427 the archive is equivalent to inserting the same values into the "name",
428 "mode", "mtime", "data" and "method" columns of a zipfile virtual table.
429
430 </p><p> If zipfile() is invoked with 2 arguments, then the entry added to
431 the archive is equivalent to that added by inserting the same two values into
432 the "name" and "data" columns of a zipfile virtual table, with all
433 other values set to NULL. If invoked with 4 arguments, it is equivalent
434 to inserting the 4 values into the "name", "mode", "mtime" and "data"
435 columns. In other words, the following pairs of queries are equivalent:
436
437 </p><div class="codeblock"><pre>SELECT zipfile(name, data) ...
438 SELECT zipfile(name, NULL, NULL, data, NULL) ...
439
440 SELECT zipfile(name, mode, mtime, data) ...
441 SELECT zipfile(name, mode, mtime, data, NULL) ...
442 </pre></div>
443
444 <p> For example, to create an archive containing two text files, "a.txt" and
445 "b.txt", containing the text "abc" and "123" respectively:
446
447 </p><div class="codeblock"><pre>WITH contents(name, data) AS (
448 VALUES('a.txt', 'abc') UNION ALL
449 VALUES('b.txt', '123')
450 )
451 SELECT zipfile(name, data) FROM contents;
452 </pre></div>
453 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/zipfile.in?m=5408af61ff">2023-06-07 13:17:51</a> UTC </small></i></p>
454