Source file : unzip-streams.ads
1 -- ________ ___ ______ ______ ___
2 -- /___..._/ |.| |.___.\ /. __ .\ __|.| ____
3 -- /../ |.| |.____/ |.|__|.| /....| __\..\
4 -- _/../___ |.| |.| === |..__..| |. = .| | = ..|
5 -- /_______/ |_| /__| /__| |_| \__\_| \__\_|
6
7 -- UnZip.Streams
8 -----------------
9 --
10 -- Variant 1: extracts, as an *input* stream, a file which is has been
11 -- compressed into a Zip archive. The Zip archive itself
12 -- can be a file, or a more general stream. Subprograms are
13 -- resembling Ada.Streams.Stream_IO, to facilitate transition.
14 --
15 -- Variant 2: extracts to an *output* stream a file which is has been
16 -- compressed into a Zip archive.
17 --
18 -- Stream directions in a nutshell...
19 --
20 -- Zip Archive | File-in-archive
21 -- -------------|-----------------
22 -- Variant 1: Input | Input
23 -- Variant 2: Input | Output
24 --
25
26 -- Legal licensing note:
27
28 -- Copyright (c) 1999 .. 2024 Gautier de Montmollin
29 -- SWITZERLAND
30
31 -- Permission is hereby granted, free of charge, to any person obtaining a copy
32 -- of this software and associated documentation files (the "Software"), to deal
33 -- in the Software without restriction, including without limitation the rights
34 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
35 -- copies of the Software, and to permit persons to whom the Software is
36 -- furnished to do so, subject to the following conditions:
37
38 -- The above copyright notice and this permission notice shall be included in
39 -- all copies or substantial portions of the Software.
40
41 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
44 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
47 -- THE SOFTWARE.
48
49 -- NB: this is the MIT License, as found on the site
50 -- http://www.opensource.org/licenses/mit-license.php
51
52 with Zip_Streams;
53
54 with Ada.IO_Exceptions;
55
56 package UnZip.Streams is
57
58 ----------------------------------------------------------------------------
59 -- ** Variant 1: contents as Input Stream ** --
60 -- ----------------------------------- --
61 -- --
62 -- Extract a Zip archive entry as an input stream. --
63 -- --
64 -- The workflow is similar to a "physical" file's: --
65 -- --
66 -- - Open z: Zipped_File_Type --
67 -- - do something with Stream(z), usually: read the data --
68 -- - Close z --
69 -- --
70 -- NB: the whole entry is unpacked into memory at Open, so it uses --
71 -- the uncompressed amount as work memory between Open and Close. --
72 ----------------------------------------------------------------------------
73
74 type Zipped_File_Type is private;
75
76 type Count is new Zip_Streams.ZS_Size_Type;
77 subtype Positive_Count is Count range 1 .. Count'Last;
78
79 -- Opens an input stream for the compressed file named Name stored
80 -- in the archive file named Archive_Name. The function Stream(..)
81 -- then gives access to the opened stream.
82
83 -- Version: Zip as a file.
84 procedure Open
85 (File : in out Zipped_File_Type; -- File-in-archive handle
86 Archive_Name : in String; -- Name of archive file
87 Name : in String; -- Name of zipped entry
88 Password : in String := ""; -- Decryption password
89 Case_sensitive : in Boolean := False;
90 Ignore_Directory : in Boolean := False -- True: will open Name in first directory found
91 );
92
93 -- Version: Zip as a stream.
94 procedure Open
95 (File : in out Zipped_File_Type; -- File-in-archive handle
96 Archive_Stream : in out Zip_Streams.Root_Zipstream_Type'Class; -- Archive's stream
97 Name : in String; -- Name of zipped entry
98 Password : in String := ""; -- Decryption password
99 Case_sensitive : in Boolean := False;
100 Ignore_Directory : in Boolean := False -- True: will open Name in first directory found
101 );
102
103 -- Same as above, but uses a the pre-loaded contents of the archive's
104 -- Central Directory; hence Archive_Info is passed instead of
105 -- Archive_Name or Archive_Stream.
106 -- You need to call Zip.Load( Archive_Info... ) prior to opening the
107 -- compressed file.
108
109 -- Version: Zip as Zip_info.
110 procedure Open
111 (File : in out Zipped_File_Type; -- File-in-archive handle
112 Archive_Info : in Zip.Zip_Info; -- Archive's Zip_info
113 Name : in String; -- Name of zipped entry
114 Password : in String := ""; -- Decryption password
115 Ignore_Directory : in Boolean := False -- True: will open Name in first directory found
116 );
117
118 procedure Close (File : in out Zipped_File_Type);
119
120 function Name (File : in Zipped_File_Type) return String;
121
122 function Is_Open (File : in Zipped_File_Type) return Boolean;
123 function End_Of_File (File : in Zipped_File_Type) return Boolean;
124
125 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
126
127 ------------------------------------------------------------------------
128 -- The function Stream gives access to the uncompressed data as input --
129 ------------------------------------------------------------------------
130 function Stream (File : Zipped_File_Type) return Stream_Access;
131
132 -- Operations on position within decompressed file
133
134 procedure Set_Index (File : in Zipped_File_Type; To : in Positive_Count);
135 function Index (File : in Zipped_File_Type) return Positive_Count;
136 function Size (File : in Zipped_File_Type) return Count;
137
138 Use_Error : exception renames Ada.IO_Exceptions.Use_Error;
139 End_Error : exception renames Ada.IO_Exceptions.End_Error;
140
141 ------------------------------------------------------------------
142 -- ** Variant 2: contents as Output Stream ** --
143 -- ------------------------------------ --
144 -- --
145 -- Extract a Zip archive entry to an available output stream. --
146 -- --
147 -- NB: the memory footprint is limited to the decompression --
148 -- structures and buffering, so the outward stream can be --
149 -- an interesting alternative to the inward stream --
150 -- (variant 1), albeit less comfortable. --
151 -- --
152 ------------------------------------------------------------------
153
154 procedure Extract
155 (Destination : in out Ada.Streams.Root_Stream_Type'Class;
156 Archive_Info : in Zip.Zip_Info; -- Archive's Zip_info
157 Entry_Name : in String; -- Name of zipped entry
158 Password : in String := ""; -- Decryption password
159 Ignore_Directory : in Boolean := False); -- True: will open Name in first directory found
160
161 private
162
163 type UZS_State is
164 (uninitialized,
165 data_uncompressed, -- In that model, all data is unzipped in one
166 -- time, into memory. If you have a smarter
167 -- idea (small buffer with tasking, write me!)
168 end_of_zip); -- We have reached the end, not yet closed
169
170 type p_String is access String;
171
172 type UnZip_Stream_Type is new Ada.Streams.Root_Stream_Type with record
173 state : UZS_State := uninitialized;
174 archive_info : Zip.Zip_Info; -- archive info (.zip file, directory)
175 file_name : p_String; -- name of zipped file to unzip from archive
176 uncompressed : p_Stream_Element_Array; -- whole uncompressed data
177 index : Ada.Streams.Stream_Element_Offset;
178 end record;
179
180 overriding procedure Read
181 (Self : in out UnZip_Stream_Type;
182 Item : out Ada.Streams.Stream_Element_Array;
183 Last : out Ada.Streams.Stream_Element_Offset);
184
185 overriding procedure Write
186 (Self : in out UnZip_Stream_Type;
187 Item : in Ada.Streams.Stream_Element_Array);
188
189 type Zipped_File_Type is access UnZip_Stream_Type;
190
191 end UnZip.Streams;
Web view of Ada source code generated by GNATHTML, project: ALI_Parse version 1.0.
Zip-Ada: Ada library for zip archive files (.zip).
Ada programming.
Some news about Zip-Ada and other Ada projects
on Gautier's blog.