Differenze tra le versioni di "Estrarre con le funzioni del API di libzip i file contenuti da un file compresso .zip"

Da Gambas-it.org - Wikipedia.
Riga 21: Riga 21:
 
  Library "libzip:2.1.0"
 
  Library "libzip:2.1.0"
 
   
 
   
  <FONT Color=gray>' ''struct zip *zip_open(const char *fn, int flags, int *zep)''
+
  <FONT Color=gray>' ''struct zip *zip_open(const char *, int, int *)''
 
  ' ''Open zip archive.''</font>
 
  ' ''Open zip archive.''</font>
  Private Extern zip_open(fn As String, flags As Integer, zep As Pointer) As Pointer
+
  Private Extern zip_open(path As String, flags As Integer, errorp As Pointer) As Pointer
 
   
 
   
  <FONT Color=gray>' ''zip_int64_t zip_get_num_entries(zip_t *archive, zip_flags_t flags)''
+
  <FONT Color=gray>' ''zip_int64_t zip_get_num_entries(zip_t *, zip_flags_t)''
 
  ' ''Get number of files in archive.''</font>
 
  ' ''Get number of files in archive.''</font>
 
  Private Extern zip_get_num_entries(archive As Pointer, flags As Integer) As Long
 
  Private Extern zip_get_num_entries(archive As Pointer, flags As Integer) As Long
 
   
 
   
  <FONT Color=gray>' ''int zip_stat_index(struct zip *za, int index, int flags, struct zip_stat *st)''
+
  <FONT Color=gray>' ''int zip_stat_index(struct zip *, int, int, struct zip_stat *)''
  ' Get information about file by index.''</font>
+
  ' ''Get information about file by index.''</font>
  Private Extern zip_stat_index(za As Pointer, index As Integer, flags As Integer, zst As Zip_stat) As Integer
+
  Private Extern zip_stat_index(archive As Pointer, index As Integer, flags As Integer, sb As Zip_stat) As Integer
 
   
 
   
  <FONT Color=gray>' ''struct zip_file * zip_fopen_index(struct zip *za, int fileno, int flags)''
+
  <FONT Color=gray>' ''struct zip_file * zip_fopen_index(struct zip *, int, int)''
  ' Open file in zip archive for reading by index''</font>
+
  ' ''Open file in zip archive for reading by index''</font>
  Private Extern zip_fopen_index(za As Pointer, fileno As Integer, flags As Integer) As Pointer
+
  Private Extern zip_fopen_index(archive As Pointer, index As Integer, flags As Integer) As Pointer
 
   
 
   
  <FONT Color=gray>' ''ssize_t zip_fread(struct zip_file *zf, void *outbuf, size_t toread)''
+
  <FONT Color=gray>' ''zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t)''
  ' Read from file.''</font>
+
  ' ''Read from file.''</font>
  Private Extern zip_fread(zf As Pointer, outbuf As Byte[], toread As Pointer) As Long
+
  Private Extern zip_fread(zfile As Pointer, buf As Byte[], nbytes As Long) As Long
 
   
 
   
  <FONT Color=gray>' ''int zip_fclose(struct zip_file *zf)''
+
  <FONT Color=gray>' ''int zip_fclose(struct zip_file *)''
  ' Close file in zip archive.''</font>
+
  ' ''Close file in zip archive.''</font>
  Private Extern zip_fclose(zf As Pointer)
+
  Private Extern zip_fclose(zfile As Pointer)
 +
 +
<FONT Color=gray>' ''int zip_close(struct zip archive)''
 +
' ''Close zip archive.''</font>
 +
Private Extern zip_close(archive As Pointer)
 
   
 
   
 
   
 
   
Riga 54: Riga 58:
 
   Dim l As Long
 
   Dim l As Long
 
   Dim fl As File
 
   Dim fl As File
   Dim buf As New Byte[100]
+
   Dim buf As New Byte[64]
 
    
 
    
 
   percorsoZip = "<FONT Color=gray>''/percorso/del/file.zip''</font>"
 
   percorsoZip = "<FONT Color=gray>''/percorso/del/file.zip''</font>"
Riga 70: Riga 74:
 
       Print "mtime:      "; zs.mtime
 
       Print "mtime:      "; zs.mtime
 
       If Right(String@(zs.name), 1) = "/" Then
 
       If Right(String@(zs.name), 1) = "/" Then
         Mkdir String@(zs.name)
+
         Continue
 
       Else
 
       Else
 
         zf = zip_fopen_index(z, i, 0)
 
         zf = zip_fopen_index(z, i, 0)
 
         If IsNull(zf) Then Error.Raise("Errore alla funzione 'zip_fopen_index()' !")
 
         If IsNull(zf) Then Error.Raise("Errore alla funzione 'zip_fopen_index()' !")
         fl = Open decompresso &/ String@(zs.name) For Create
+
         fl = Open decompresso &/ File.Name(String@(zs.name)) For Create
 
         While l < zs.size
 
         While l < zs.size
           lun = zip_fread(zf, buf, 100)
+
           lun = zip_fread(zf, buf, 64)
 
           If lun < 0 Then Error.Raise("Errore nella lettura del file !")
 
           If lun < 0 Then Error.Raise("Errore nella lettura del file !")
 
           buf.Write(fl, 0, lun)
 
           buf.Write(fl, 0, lun)
 
           l += lun
 
           l += lun
 
         Wend
 
         Wend
 +
        fl.Close
 +
        zip_fclose(zf)
 
       Endif
 
       Endif
 
     Endif
 
     Endif
 
            
 
            
 
   Next
 
   Next
   
+
    
   fl.Close
+
   zip_close(z)
   zip_fclose(zf)
 
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione delle 18:45, 3 lug 2017

La libreria 'libzip consente di gestire file compressi .zip.

Per poter fruire in gambas delle risorse della libreria libzip, è necessario installare e richiamare la libreria dinamica condivisa: libzip.so.2.1.0


Mostriamo un esempio pratico per estrarre uno o più file contenuti in un file compresso .zip:

Public Struct zip_stat
  valid As Long
  name As Pointer
  index As Long
  size As Long
  comp_size As Long
  mtime As Long
  crc As Integer
  comp_method As Short
  encryption_method As Short
  flags As Integer
End Struct


Library "libzip:2.1.0"

' struct zip *zip_open(const char *, int, int *)
' Open zip archive.
Private Extern zip_open(path As String, flags As Integer, errorp As Pointer) As Pointer

' zip_int64_t zip_get_num_entries(zip_t *, zip_flags_t)
' Get number of files in archive.
Private Extern zip_get_num_entries(archive As Pointer, flags As Integer) As Long

' int zip_stat_index(struct zip *, int, int, struct zip_stat *)
' Get information about file by index.
Private Extern zip_stat_index(archive As Pointer, index As Integer, flags As Integer, sb As Zip_stat) As Integer

' struct zip_file * zip_fopen_index(struct zip *, int, int)
' Open file in zip archive for reading by index
Private Extern zip_fopen_index(archive As Pointer, index As Integer, flags As Integer) As Pointer

' zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t)
' Read from file.
Private Extern zip_fread(zfile As Pointer, buf As Byte[], nbytes As Long) As Long

' int zip_fclose(struct zip_file *)
' Close file in zip archive.
Private Extern zip_fclose(zfile As Pointer)

' int zip_close(struct zip archive)
' Close zip archive.
Private Extern zip_close(archive As Pointer)


Public Sub Main()
 
 Dim percorsoZip, decompresso As String
 Dim z, zf As Pointer
 Dim i, lun As Integer
 Dim zs As New Zip_stat
 Dim l As Long
 Dim fl As File
 Dim buf As New Byte[64]
 
  percorsoZip = "/percorso/del/file.zip"
  decompresso = "/percorso/ove/saranno/estratti/i/file"
  
  z = zip_open(percorsoZip, 0, 0)
  If IsNull(z) Then Error.Raise("Impossibile aprire un file '.zip' !")
  
  For i = 0 To zip_get_num_entries(z, 0) - 1
     
    If zip_stat_index(z, i, 0, zs) = 0 Then
      lun = Len(String@(zs.name))
      Print "Nome:       "; String@(zs.name)
      Print "Dimensione: "; zs.size
      Print "mtime:      "; zs.mtime
      If Right(String@(zs.name), 1) = "/" Then
        Continue
      Else
        zf = zip_fopen_index(z, i, 0)
        If IsNull(zf) Then Error.Raise("Errore alla funzione 'zip_fopen_index()' !")
        fl = Open decompresso &/ File.Name(String@(zs.name)) For Create
        While l < zs.size
          lun = zip_fread(zf, buf, 64)
          If lun < 0 Then Error.Raise("Errore nella lettura del file !")
          buf.Write(fl, 0, lun)
          l += lun
        Wend
        fl.Close
        zip_fclose(zf)
      Endif
    Endif
          
  Next
  
  zip_close(z)
  
End



Riferimenti