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.
(10 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | La libreria | + | 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 condivisa: "''libzip.so.4.0'' " oppure "''libzip.so.5.0'' ". | ||
Mostriamo un esempio pratico per estrarre uno o più file contenuti in un file compresso .zip: | Mostriamo un esempio pratico per estrarre uno o più file contenuti in un file compresso .zip: | ||
+ | Library "libzip:4.0" <FONT Color=gray>' ''oppure: libzip:5.0''</font> | ||
+ | |||
Public Struct zip_stat | Public Struct zip_stat | ||
valid As Long | valid As Long | ||
Riga 18: | Riga 19: | ||
End Struct | End Struct | ||
− | + | <FONT Color=gray>' ''struct zip *zip_open(const char *, int, int *)'' | |
− | |||
− | |||
− | <FONT Color=gray>' ''struct zip *zip_open(const char * | ||
' ''Open zip archive.''</font> | ' ''Open zip archive.''</font> | ||
− | Private Extern zip_open( | + | 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 * | + | <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 * | + | <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( | + | 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 * | + | <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( | + | Private Extern zip_fopen_index(archive As Pointer, index As Integer, flags As Integer) As Pointer |
− | <FONT Color=gray>' '' | + | <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( | + | Private Extern zip_fread(zfile As Pointer, buf As Byte[], nbytes As Long) As Long |
− | <FONT Color=gray>' ''int zip_fclose(struct zip_file * | + | <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( | + | Private Extern zip_fclose(zfile As Pointer) As Integer |
+ | |||
+ | <FONT Color=gray>' ''int zip_close(struct zip *)'' | ||
+ | ' ''Close zip archive.''</font> | ||
+ | Private Extern zip_close(archive As Pointer) As Integer | ||
'''Public''' Sub Main() | '''Public''' Sub Main() | ||
− | Dim percorsoZip, decompresso As String | + | Dim percorsoZip, decompresso, zsname As String |
Dim z, zf As Pointer | Dim z, zf As Pointer | ||
Dim i, lun As Integer | Dim i, lun As Integer | ||
Riga 54: | Riga 56: | ||
Dim l As Long | Dim l As Long | ||
Dim fl As File | Dim fl As File | ||
− | Dim buf As New Byte[ | + | Dim buf As New Byte[64] |
− | + | percorsoZip = "<FONT Color=gray>''/percorso/del/file.zip''</font>" | |
− | + | decompresso = "<FONT Color=gray>''/cartella/ove/saranno/estratti/i/file''</font>" | |
+ | |||
+ | z = zip_open(percorsoZip, 0, 0) | ||
+ | If z == 0 Then | ||
+ | zip_close(z) | ||
+ | Error.Raise("Impossibile aprire un file '.zip' !") | ||
+ | Endif | ||
− | + | For i = 0 To zip_get_num_entries(z, 0) - 1 | |
− | + | If zip_stat_index(z, i, 0, zs) = 0 Then | |
+ | zsname = String@(zs.name) | ||
+ | Print "Nome: "; zsname | ||
+ | Print "Dimensione: "; zs.size | ||
+ | Print "mtime: "; zs.mtime | ||
+ | If Right(zsname, 1) = "/" Then | ||
+ | Continue | ||
+ | Else | ||
+ | zf = zip_fopen_index(z, i, 0) | ||
+ | If zf == 0 Then | ||
+ | zip_fclose(zf) | ||
+ | Error.Raise("Errore alla funzione 'zip_fopen_index()' !") | ||
+ | Endif | ||
+ | fl = Open decompresso &/ File.Name(zsname) For Create | ||
+ | l = 0 | ||
+ | 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''' | '''End''' | ||
+ | In quest'altro esempio, simile al precedente, sarà mostrato in apposita ''ProcessBar'' lo stato di avanzamento dei file decompressi, contenuti in un unico file compresso zip. | ||
+ | Private Const ZIP As String = "<FONT Color=gray>''/percorso/del/file.zip''</font>" | ||
+ | Private Const UNZIP As String = "<FONT Color=gray>''/cartella/ove/saranno/salvati/i/file/decompressi''</font>" | ||
+ | Private z As Pointer | ||
+ | Private entries As Long | ||
+ | |||
+ | |||
+ | Library "libzip:4.0" <FONT Color=gray>' ''oppure: libzip:5.0''</font> | ||
+ | |||
+ | 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 | ||
+ | |||
+ | <FONT Color=gray>' ''struct zip *zip_open(const char *, int, int *)'' | ||
+ | ' ''Open zip archive.''</font> | ||
+ | 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 *, zip_flags_t)'' | ||
+ | ' ''Get number of files in archive.''</font> | ||
+ | Private Extern zip_get_num_entries(archive As Pointer, flags As Integer) As Long | ||
+ | |||
+ | <FONT Color=gray>' ''int zip_stat_index(struct zip *, int, int, struct zip_stat *)'' | ||
+ | ' ''Get information about file by index.''</font> | ||
+ | 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 *, int, int)'' | ||
+ | ' ''Open file in zip archive for reading by index''</font> | ||
+ | Private Extern zip_fopen_index(archive As Pointer, index As Integer, flags As Integer) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t)'' | ||
+ | ' ''Read from file.''</font> | ||
+ | Private Extern zip_fread(zfile As Pointer, buf As Byte[], nbytes As Long) As Long | ||
+ | |||
+ | <FONT Color=gray>' ''int zip_fclose(struct zip_file *)'' | ||
+ | ' ''Close file in zip archive.''</font> | ||
+ | Private Extern zip_fclose(zfile As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''int zip_close(struct zip *)'' | ||
+ | ' ''Close zip archive.''</font> | ||
+ | Private Extern zip_close(archive As Pointer) As Integer | ||
+ | |||
+ | |||
+ | Public Sub Form_Open() | ||
+ | |||
+ | z = zip_open(ZIP, 0, 0) | ||
+ | If z == 0 Then | ||
+ | zip_close(z) | ||
+ | Error.Raise("Impossibile aprire un file '.zip' !") | ||
+ | Endif | ||
+ | |||
+ | entries = zip_get_num_entries(z, 0) | ||
+ | Print "Numero file da decomprimere: "; entries | ||
+ | |||
+ | End | ||
+ | |||
+ | Public Sub Button1_Click() | ||
+ | |||
+ | Dim zf As Pointer | ||
+ | Dim i, lun As Integer | ||
+ | Dim zsname As String | ||
+ | Dim zs As New Zip_stat | ||
+ | Dim l As Long | ||
+ | Dim fl As File | ||
+ | Dim buf As New Byte[64] | ||
+ | |||
+ | For i = 0 To entries - 1 | ||
+ | If zip_stat_index(z, i, 0, zs) = 0 Then | ||
+ | zsname = String@(zs.name) | ||
+ | Print gb.NewLine; i + 1 | ||
+ | Print "Nome: "; zsname | ||
+ | Print "Dimensione: "; zs.size | ||
+ | If Right(zsname, 1) = "/" Then | ||
+ | Continue | ||
+ | Else | ||
+ | zf = zip_fopen_index(z, i, 0) | ||
+ | If zf == 0 Then | ||
+ | zip_fclose(zf) | ||
+ | Error.Raise("Error !") | ||
+ | Endif | ||
+ | fl = Open UNZIP &/ File.Name(zsname) For Create | ||
+ | l = 0 | ||
+ | While l < zs.size | ||
+ | lun = zip_fread(zf, buf, 64) | ||
+ | If lun < 0 Then Error.Raise("Error !") | ||
+ | buf.Write(fl, 0, lun) | ||
+ | l += lun | ||
+ | Wend | ||
+ | fl.Close | ||
+ | zip_fclose(zf) | ||
+ | Endif | ||
+ | Endif | ||
+ | ProgressBar1.Value = (i + 1) / entries | ||
+ | Wait 0.3 | ||
+ | Next | ||
+ | |||
+ | zip_close(z) | ||
+ | |||
+ | End | ||
Riga 97: | Riga 212: | ||
* http://www.nih.at/libzip | * http://www.nih.at/libzip | ||
* http://sourcecodebrowser.com/libzip/0.9/index.html | * http://sourcecodebrowser.com/libzip/0.9/index.html | ||
+ | * https://linux.die.net/man/3/libzip |
Versione attuale delle 05:23, 19 mag 2023
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 condivisa: "libzip.so.4.0 " oppure "libzip.so.5.0 ".
Mostriamo un esempio pratico per estrarre uno o più file contenuti in un file compresso .zip:
Library "libzip:4.0" ' oppure: libzip:5.0 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 ' 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) As Integer ' int zip_close(struct zip *) ' Close zip archive. Private Extern zip_close(archive As Pointer) As Integer Public Sub Main() Dim percorsoZip, decompresso, zsname 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 = "/cartella/ove/saranno/estratti/i/file" z = zip_open(percorsoZip, 0, 0) If z == 0 Then zip_close(z) Error.Raise("Impossibile aprire un file '.zip' !") Endif For i = 0 To zip_get_num_entries(z, 0) - 1 If zip_stat_index(z, i, 0, zs) = 0 Then zsname = String@(zs.name) Print "Nome: "; zsname Print "Dimensione: "; zs.size Print "mtime: "; zs.mtime If Right(zsname, 1) = "/" Then Continue Else zf = zip_fopen_index(z, i, 0) If zf == 0 Then zip_fclose(zf) Error.Raise("Errore alla funzione 'zip_fopen_index()' !") Endif fl = Open decompresso &/ File.Name(zsname) For Create l = 0 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
In quest'altro esempio, simile al precedente, sarà mostrato in apposita ProcessBar lo stato di avanzamento dei file decompressi, contenuti in un unico file compresso zip.
Private Const ZIP As String = "/percorso/del/file.zip" Private Const UNZIP As String = "/cartella/ove/saranno/salvati/i/file/decompressi" Private z As Pointer Private entries As Long Library "libzip:4.0" ' oppure: libzip:5.0 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 ' 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) As Integer ' int zip_close(struct zip *) ' Close zip archive. Private Extern zip_close(archive As Pointer) As Integer Public Sub Form_Open() z = zip_open(ZIP, 0, 0) If z == 0 Then zip_close(z) Error.Raise("Impossibile aprire un file '.zip' !") Endif entries = zip_get_num_entries(z, 0) Print "Numero file da decomprimere: "; entries End Public Sub Button1_Click() Dim zf As Pointer Dim i, lun As Integer Dim zsname As String Dim zs As New Zip_stat Dim l As Long Dim fl As File Dim buf As New Byte[64] For i = 0 To entries - 1 If zip_stat_index(z, i, 0, zs) = 0 Then zsname = String@(zs.name) Print gb.NewLine; i + 1 Print "Nome: "; zsname Print "Dimensione: "; zs.size If Right(zsname, 1) = "/" Then Continue Else zf = zip_fopen_index(z, i, 0) If zf == 0 Then zip_fclose(zf) Error.Raise("Error !") Endif fl = Open UNZIP &/ File.Name(zsname) For Create l = 0 While l < zs.size lun = zip_fread(zf, buf, 64) If lun < 0 Then Error.Raise("Error !") buf.Write(fl, 0, lun) l += lun Wend fl.Close zip_fclose(zf) Endif Endif ProgressBar1.Value = (i + 1) / entries Wait 0.3 Next zip_close(z) End