PureZIP library : ZIP / UNZIP files

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Thanks! I tried to import an older static lib, but i get unresolved external symbol _errno. Is there any way of getting rid of that error?

Code: Select all

; Import-File created by Lib2PBImport
; Libname: zlibstat.lib
; created: 2007/07/09  12:15

Import "zlibstat.lib"
  gzclearerr(a.l) As "_gzclearerr@4"
  gzclose(a.l) As "_gzclose@4"
  gzdopen(a.l,b.l) As "_gzdopen@8"
  gzeof(a.l) As "_gzeof@4"
  gzerror(a.l,b.l) As "_gzerror@8"
  gzflush(a.l,b.l) As "_gzflush@8"
  gzgetc(a.l) As "_gzgetc@4"
  gzgets(a.l,b.l,c.l) As "_gzgets@12"
  gzopen(a.l,b.l) As "_gzopen@8"
  gzprintf() As "_gzprintf"
  gzputc(a.l,b.l) As "_gzputc@8"
  gzputs(a.l,b.l) As "_gzputs@8"
  gzread(a.l,b.l,c.l) As "_gzread@12"
  gzrewind(a.l) As "_gzrewind@4"
  gzseek(a.l,b.l,c.l) As "_gzseek@12"
  gzsetparams(a.l,b.l,c.l) As "_gzsetparams@12"
  gztell(a.l) As "_gztell@4"
  gzungetc(a.l,b.l) As "_gzungetc@8"
  gzwrite(a.l,b.l,c.l) As "_gzwrite@12"
EndImport

;-test gzio, read/write .gz files
Procedure test_gzio()
  Debug "# start gz input / output"
  txt$="another text for testing"
  TESTFILE$="e:\test\gztest.txt.gz" ;- !! change path !!
  Debug "- first output"
  ;-write gz
  fileHdl = gzopen(@TESTFILE$, @"wb9");Type of access permitted "wb" or "rb" + comressionlevel and strategy i.e. "wb9f"
  ;"r"
  ;Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
  ;"w"
  ;Opens an empty file for writing. If the given file exists, its contents are destroyed.
  ;this are the same parameters as for the fopen function in c++
  ;
  ;9 for max compression, 0 for no comression (0 can be good for controlling with an editor what happened).
  ;'f' for filtered data as in "wb6f", 'h' for
  ;Huffman only compression as in "wb1h", Or 'R' For run-length encoding ...
  ;See the description of deflateInit2 in zlib.h  for more information for these parameters
  Debug "fileHdl for write= " + Str(fileHdl)
  zerr=gzputc(fileHdl, Asc("H"))
  Debug "err gzputc " + Str(zerr) + " = " + "Asc('H')" + " = " + Str(Asc("H"))
  len=1
  zerr=gzputs(fileHdl, @"ello |");returns length of the uncompressed string
  Debug "err gzputs " + Str(zerr)
  len + zerr
  xtmp$=txt$ + Str(i) + " "
  zerr=gzwrite(fileHdl, @xtmp$,Len(txt$));returns length of the uncompressed
  Debug "err gzwrite " + Str(zerr)                     ;string if successfull and a zerror if not
  len + zerr
  Debug "Bytes written: " + Str(len)
  zerr=gzclose(fileHdl)
  Debug ""
 
  ;-read gz
  Debug "- then input"
  ;read the uncompressed length
  ;  must not be done, you can allocate a buffer (don´t even have to be much, in my test it worked
  ;                                               with 1 Byte for 20 loops of obove gzwrite !?)
  ;  and do a loop until Zgzread(fileHdl,@Buf$,length-1) returns zero
  ;  look at the gz_uncompress function in minigzip.c from the zlib source this
  ;  demonstrates reading compressed data without knowing the uncompressed length
  If OpenFile(0,TESTFILE$)
    length=Lof(0)
    FileSeek(0,length-4)
    length=ReadLong(0);the length of the uncompressed data is stored in the last 4 bytes
    Debug "length " + Str(length)
    CloseFile(0)
  EndIf
 
  fileHdl = gzopen(@TESTFILE$, @"rb");"rb" for read byte, returns the file handle
  Debug "fileHdl for read= " + Str(fileHdl)
  Buf$=Space(length)
  len=gzread(fileHdl,@Buf$,length)
  Debug PeekS(@Buf$)
  Debug "Bytes read: " + Str(len);if len < 0 then an error occured
 
  zerr=gzclose(fileHdl)
EndProcedure

P.S. Where can i get the latest zlib static lib?

Thank you for the help.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Inf0Byt3 wrote:Thanks! I tried to import an older static lib, but i get unresolved external symbol _errno. Is there any way of getting rid of that error?
I don't know
Maybe you need to import some other library, like MSVCRT ... ?
Inf0Byt3 wrote:P.S. Where can i get the latest zlib static lib?
I use this one : http://www.winimage.com/zLibDll/zlib123dll.zip , and I get the same error ...

Try using this library http://people.freenet.de/zlibforpb/ZuserLIB.zip with this code :

Code: Select all

;-test gzio, read/write .gz files 
Procedure test_gzio() 
  Debug "# start gz input / output" 
  txt$="another text for testing" 
  TESTFILE$="e:\test\gztest.txt.gz" ;- !! change path !! 
  Debug "- first output" 
  ;-write gz 
  fileHdl = Zgzopen(@TESTFILE$, @"wb9");Type of access permitted "wb" or "rb" + comressionlevel and strategy i.e. "wb9f" 
  ;"r" 
  ;Opens for reading. If the file does not exist or cannot be found, the fopen call fails. 
  ;"w" 
  ;Opens an empty file for writing. If the given file exists, its contents are destroyed. 
  ;this are the same parameters as for the fopen function in c++ 
  ; 
  ;9 for max compression, 0 for no comression (0 can be good for controlling with an editor what happened). 
  ;'f' for filtered data as in "wb6f", 'h' for 
  ;Huffman only compression as in "wb1h", Or 'R' For run-length encoding ... 
  ;See the description of deflateInit2 in zlib.h  for more information for these parameters 
  Debug "fileHdl for write= " + Str(fileHdl) 
  zerr=Zgzputc(fileHdl, Asc("H")) 
  Debug "err gzputc " + Str(zerr) + " = " + "Asc('H')" + " = " + Str(Asc("H")) 
  len=1 
  zerr=Zgzputs(fileHdl, @"ello |");returns length of the uncompressed string 
  Debug "err gzputs " + Str(zerr) 
  len + zerr 
  xtmp$=txt$ + Str(i) + " " 
  zerr=Zgzwrite(fileHdl, @xtmp$,Len(txt$));returns length of the uncompressed 
  Debug "err gzwrite " + Str(zerr)                     ;string if successfull and a zerror if not 
  len + zerr 
  Debug "Bytes written: " + Str(len) 
  zerr=Zgzclose(fileHdl) 
  Debug "" 
  
  ;-read gz 
  Debug "- then input" 
  ;read the uncompressed length 
  ;  must not be done, you can allocate a buffer (don´t even have to be much, in my test it worked 
  ;                                               with 1 Byte for 20 loops of obove gzwrite !?) 
  ;  and do a loop until Zgzread(fileHdl,@Buf$,length-1) returns zero 
  ;  look at the gz_uncompress function in minigzip.c from the zlib source this 
  ;  demonstrates reading compressed data without knowing the uncompressed length 
  If OpenFile(0,TESTFILE$) 
    length=Lof(0) 
    FileSeek(0,length-4) 
    length=ReadLong(0);the length of the uncompressed data is stored in the last 4 bytes 
    Debug "length " + Str(length) 
    CloseFile(0) 
  EndIf 
  
  fileHdl = Zgzopen(@TESTFILE$, @"rb");"rb" for read byte, returns the file handle 
  Debug "fileHdl for read= " + Str(fileHdl) 
  Buf$=Space(length) 
  len=Zgzread(fileHdl,@Buf$,length) 
  Debug PeekS(@Buf$) 
  Debug "Bytes read: " + Str(len);if len < 0 then an error occured 
  
  zerr=Zgzclose(fileHdl) 
EndProcedure
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Thanks! I'll test the code and report back :).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

@Inf0Byt3 :
Please also try this static library : http://freenet-homepage.de/gnozal/PBLIB ... c_GZip.zip
So you don't need any user library.

Code: Select all

;
; Need ZlibStatic.lib
;
Import "ZlibStatic.lib" 
  gzclearerr(a.l) As "_gzclearerr@4" 
  gzclose(a.l) As "_gzclose@4" 
  gzdopen(a.l,b.l) As "_gzdopen@8" 
  gzeof(a.l) As "_gzeof@4" 
  gzerror(a.l,b.l) As "_gzerror@8" 
  gzflush(a.l,b.l) As "_gzflush@8" 
  gzgetc(a.l) As "_gzgetc@4" 
  gzgets(a.l,b.l,c.l) As "_gzgets@12" 
  gzopen(a.l,b.l) As "_gzopen@8" 
  gzprintf() As "_gzprintf" 
  gzputc(a.l,b.l) As "_gzputc@8" 
  gzputs(a.l,b.l) As "_gzputs@8" 
  gzread(a.l,b.l,c.l) As "_gzread@12" 
  gzrewind(a.l) As "_gzrewind@4" 
  gzseek(a.l,b.l,c.l) As "_gzseek@12" 
  gzsetparams(a.l,b.l,c.l) As "_gzsetparams@12" 
  gztell(a.l) As "_gztell@4" 
  gzungetc(a.l,b.l) As "_gzungetc@8" 
  gzwrite(a.l,b.l,c.l) As "_gzwrite@12" 
EndImport 

;-test gzio, read/write .gz files 
Procedure test_gzio() 
  Debug "# start gz input / output" 
  txt$="another text for testing" 
  TESTFILE$="e:\test\gztest.txt.gz" ;- !! change path !! 
  Debug "- first output" 
  ;-write gz 
  fileHdl = gzopen(@TESTFILE$, @"wb9");Type of access permitted "wb" or "rb" + comressionlevel and strategy i.e. "wb9f" 
  ;"r" 
  ;Opens for reading. If the file does not exist or cannot be found, the fopen call fails. 
  ;"w" 
  ;Opens an empty file for writing. If the given file exists, its contents are destroyed. 
  ;this are the same parameters as for the fopen function in c++ 
  ; 
  ;9 for max compression, 0 for no comression (0 can be good for controlling with an editor what happened). 
  ;'f' for filtered data as in "wb6f", 'h' for 
  ;Huffman only compression as in "wb1h", Or 'R' For run-length encoding ... 
  ;See the description of deflateInit2 in zlib.h  for more information for these parameters 
  Debug "fileHdl for write= " + Str(fileHdl) 
  zerr=gzputc(fileHdl, Asc("H")) 
  Debug "err gzputc " + Str(zerr) + " = " + "Asc('H')" + " = " + Str(Asc("H")) 
  len=1 
  zerr=gzputs(fileHdl, @"ello |");returns length of the uncompressed string 
  Debug "err gzputs " + Str(zerr) 
  len + zerr 
  xtmp$=txt$ + Str(i) + " " 
  zerr=gzwrite(fileHdl, @xtmp$,Len(txt$));returns length of the uncompressed 
  Debug "err gzwrite " + Str(zerr)                     ;string if successfull and a zerror if not 
  len + zerr 
  Debug "Bytes written: " + Str(len) 
  zerr=gzclose(fileHdl) 
  Debug "" 
  
  ;-read gz 
  Debug "- then input" 
  ;read the uncompressed length 
  ;  must not be done, you can allocate a buffer (don´t even have to be much, in my test it worked 
  ;                                               with 1 Byte for 20 loops of obove gzwrite !?) 
  ;  and do a loop until Zgzread(fileHdl,@Buf$,length-1) returns zero 
  ;  look at the gz_uncompress function in minigzip.c from the zlib source this 
  ;  demonstrates reading compressed data without knowing the uncompressed length 
  If OpenFile(0,TESTFILE$) 
    length=Lof(0) 
    FileSeek(0,length-4) 
    length=ReadLong(0);the length of the uncompressed data is stored in the last 4 bytes 
    Debug "length " + Str(length) 
    CloseFile(0) 
  EndIf 
  
  fileHdl = gzopen(@TESTFILE$, @"rb");"rb" for read byte, returns the file handle 
  Debug "fileHdl for read= " + Str(fileHdl) 
  Buf$=Space(length) 
  len=gzread(fileHdl,@Buf$,length) 
  Debug PeekS(@Buf$) 
  Debug "Bytes read: " + Str(len);if len < 0 then an error occured 
  
  zerr=gzclose(fileHdl) 
EndProcedure
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

This works flawlessly! Thank you for your invaluable help! 8)
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
String
New User
New User
Posts: 7
Joined: Sat May 26, 2007 9:48 pm

Post by String »

Hello Gnozal
Write-Protected files are ignored If a password is used.

PureZIP- v1.85 PB4.0x

Code: Select all

  File1.s = "c:\Temp\Test\TextA.txt"
  File2.s = "c:\Temp\Test\TextB.txt"
  If MakeSureDirectoryPathExists_(@File1)
    If OpenFile(1,File1)
      WriteString(1,"OK")
      CloseFile(1)      
    EndIf
    If OpenFile(2,File2)
      WriteString(2,"ReadOnly")
      CloseFile(2)
      SetFileAttributes_(File2 ,#FILE_ATTRIBUTE_READONLY)
    EndIf
    
    Debug  PureZIP_AddFiles("C:\Temp\test1.zip", "c:\Temp\Test\*.*", #PureZIP_StorePathRelative, #PureZIP_Recursive)
    
    PureZIP_SetArchivePassword("PW") 
    Debug  PureZIP_AddFiles( "C:\Temp\test2.zip", "c:\Temp\Test\*.*", #PureZIP_StorePathRelative, #PureZIP_Recursive)
 
  EndIf 
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

String wrote:Hello Gnozal
Write-Protected files are ignored If a password is used.
Confirmed.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Update (Both libs)

Changes :

- fixed read-only files not being added to password protected archives
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
String
New User
New User
Posts: 7
Joined: Sat May 26, 2007 9:48 pm

Post by String »

OK thanks
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

BETA release

Changes :
- Added support for non english filenames, for example with german umlauts 'ü' or french accents 'é' ...

Please test all functions, thanks.
Link : http://freenet-homepage.de/gnozal/PureZIP_BETA.zip (PB4.0x ANSI userlib)
Alternative URL : (Easy-share)

Note : there are some problems with freenet.de right now. Using a download manager (FlashGet) worked for me.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Update (Both libs)

Changes :

- Added support for non english filenames, for example with german umlauts 'ü' or french accents 'é' ...
- Removed functions PureZIP_AsciiDosUS_To_AsciiWinWestern() and PureZIP_AsciiDosLatinUS_To_AsciiWinLatin1() : no longer necessary

Note : there are some problems with freenet.de right now. Alternative URL for both libs : Easy-share
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Thank you gnozal, i'll be testing it today and report back if I find any bugs.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

PureZIP DLL

I have build a PureZIP DLL based on the PureZIP 1.90 userlib code.
I did not test it much ...
No help included, only a purebasic wrapper.

If you are interested, please test it and report any problems.
If you plan to use it with another language, please post the include.
Thanks.

Download DLL : http://freenet-homepage.de/gnozal/PureZIP_DLL.zip
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
abc123
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Apr 18, 2007 9:27 pm

Post by abc123 »

Hi, i would be nice if something like CheckPassword function is available! :)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

abc123 wrote:Hi, i would be nice if something like CheckPassword function is available! :)
Help file says :
Structure PureZIP_FileInfo
Version.l ; version made by
VersionNeeded.l ; version needed to extract
flag.l ; general purpose bit flag [if bit 0 is set the file is encrypted]
CompressionMethod.l ; compression method [0 - 10]
dosDate.l ; last mod file date in Dos format [do not use !]
Crc32.l ; crc-32
CompressedSize.l ; compressed size
unCompressedSize.l ; uncompressed size
SizeFilename.l ; filename length
SizeFileExtra.l ; extra field length
SizeFileComment.l ; file comment length
DiskNumStart.l ; disk number start
internal_fa.l ; internal file attributes
external_fa.l ; external file attributes (packed file attributes)
tmu_date.tm_date ; file date (see tm_date structure) [use this to get filedate!]
FileName.s ; filename
EndStructure
So you can check if an archive is password protected using the file information structure :

Code: Select all

      PureZIP_Archive_FileInfo(@myFileinfo) 
      
      If myFileinfo\flag & %1 
        IsCrypted = #True 
      Else 
        IsCrypted = #False 
      EndIf 
Complete procedure :

Code: Select all

Procedure CheckPassword(myZipFile$) 
  
  Protected myFileinfo.PureZIP_FileInfo 
  Protected IsCrypted.l 
  Protected Input$ 
  
  If PureZIP_Archive_Read(myZipFile$) 
    
    If PureZIP_Archive_FindFirst() = #UNZ_OK 
      
      PureZIP_Archive_FileInfo(@myFileinfo) 
      
      If myFileinfo\flag & %1 
        IsCrypted = #True 
      Else 
        IsCrypted = #False 
      EndIf 
      
    EndIf 
    
    PureZIP_Archive_Close() 
    
  EndIf 
  
  If IsCrypted = #False 
    ProcedureReturn #True 
  EndIf 
  
  Input$ = InputRequester("Zip is crypted", "Password:", "") 
  
  If Input$ > "" 
    PureZIP_SetArchivePassword(Input$) 
    ProcedureReturn #True 
  Else 
    ProcedureReturn #False 
  EndIf 
  
EndProcedure
For more information, please see help file.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply