1. --  ________  ___   ______       ______     ___ 
  2.  -- /___ .. ._/  |.|   |.___.\     /. __ .\  __|.|   ____ 
  3.  --    / .. /    |.|   |.____/     |.|__|.| / .. ..|  __\ .. \ 
  4.  --  _/ .. /___  |.|   |.|    ===  | .. __ .. ||. = .| | = .. | 
  5.  -- /_______/  |_|  /__|        /__|  |_| \__\_|  \__\_| 
  6.  
  7.  -- UnZip.Streams 
  8.  ---------------- 
  9.  -- Extracts, as a stream, a file which is has been compressed into a Zip archive. 
  10.  -- The Zip archive itself (the input) can be a file or a more general stream. 
  11.  -- This package is resembling Ada.Streams.Stream_IO, to facilitate transition. 
  12.  
  13. with Zip, Zip_Streams; 
  14.  
  15. with Ada.Streams.Stream_IO, Ada.IO_Exceptions; 
  16.  
  17. package UnZip.Streams is 
  18.  
  19.    subtype Stream_Access is Ada.Streams.Stream_IO.Stream_Access; 
  20.  
  21.    type Zipped_File_Type is private; 
  22.  
  23.    -- Opens an input stream for the compressed file named Name stored 
  24.    -- in the archive file named Archive_Name. The function Stream ( .. ) 
  25.    -- then gives access to the opened stream. 
  26.  
  27.    -- Version : Zip as a file 
  28.    procedure Open 
  29.      (File            : in out Zipped_File_Type; -- File - in - archive handle 
  30.       Archive_Name    :        String;               -- Name of archive file 
  31.       Name            :        String;               -- Name of zipped entry 
  32.       Password        :        String := "";         -- Decryption password 
  33.       Case_sensitive  :        Boolean := False 
  34. ); 
  35.  
  36.    -- Version : Zip as a stream 
  37.    procedure Open 
  38.      (File            : in out Zipped_File_Type; -- File - in - archive handle 
  39.       Archive_Stream  :        Zip_Streams.Zipstream_Class; -- Archive's stream 
  40.       Name            :        String;               -- Name of zipped entry 
  41.       Password        :        String := "";         -- Decryption password 
  42.       Case_sensitive  :        Boolean := False 
  43. ); 
  44.  
  45.    -- Same as above, but uses a the pre - loaded contents of the archive's 
  46.    -- Central Directory; hence Archive_Info is passed instead of 
  47.    -- Archive_Name or Archive_Stream. 
  48.    -- You need to call Zip.Load (Archive_Info .. .) prior to opening the 
  49.    -- compressed file. 
  50.  
  51.    procedure Open 
  52.      (File            : in out Zipped_File_Type; -- File - in - archive handle 
  53.       Archive_Info    :        Zip.Zip_info;         -- Archive's Zip_info 
  54.       Name            :        String;               -- Name of zipped entry 
  55.       Password        :        String := "";         -- Decryption password 
  56.       Case_sensitive  :        Boolean := False 
  57. ); 
  58.  
  59.    procedure Close (File  : in out Zipped_File_Type); 
  60.  
  61.    function Is_Open     (File  : Zipped_File_Type) return Boolean; 
  62.    function End_Of_File (File  : Zipped_File_Type) return Boolean; 
  63.  
  64.    function Stream (File  : Zipped_File_Type) return Stream_Access; 
  65.  
  66.    Use_Error     : exception renames Ada.IO_Exceptions.Use_Error; 
  67.    End_Error     : exception renames Ada.IO_Exceptions.End_Error; 
  68.  
  69. private 
  70.  
  71.    type UZS_state is ( 
  72.       uninitialized, 
  73.       data_uncompressed, -- In that model, all data is unzipped in one 
  74.                          --   time, into memory. If you have a smarter 
  75.                          --   idea (small buffer with tasking, write me!) 
  76.       end_of_zip         -- We have reached the end, not yet closed 
  77. ); 
  78.  
  79.    type p_String is access String; 
  80.  
  81.    type UnZip_Stream_Type is new Ada.Streams.Root_Stream_Type with record 
  82.       state         : UZS_state := uninitialized; 
  83.       archive_info  : Zip.Zip_info; -- archive info (.zip file, directory) 
  84.       delete_info_on_closing  : Boolean; 
  85.       file_name     : p_String; -- name of zipped file to unzip from archive 
  86.       Uncompressed  : p_Stream_Element_Array; -- whole uncompressed data 
  87.       index         : Ada.Streams.Stream_Element_Offset; 
  88.    end record; 
  89.  
  90.    overriding procedure Read 
  91.      (UnZip_Stream : in out UnZip_Stream_Type; 
  92.       Item         :    out Ada.Streams.Stream_Element_Array; 
  93.       Last         :    out Ada.Streams.Stream_Element_Offset); 
  94.  
  95.    overriding procedure Write 
  96.      (UnZip_Stream : in out UnZip_Stream_Type; 
  97.       Item         :        Ada.Streams.Stream_Element_Array); 
  98.  
  99.    type Zipped_File_Type is access UnZip_Stream_Type; 
  100.  
  101. end UnZip.Streams;