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 »

Hehe, well I must appologise again for my superficiality. When I tried the code you posted, I just forgot to add the SetCurrentDirectory() line, and poor PureBasic was not finding the files. Thank you again for having patience with me, I know I can be a pain in the a** sometimes.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Micko
Enthusiast
Enthusiast
Posts: 244
Joined: Thu May 24, 2007 7:36 pm
Location: Senegal
Contact:

extract encrypt zip file

Post by Micko »

hi gnozal

Code: Select all

zipfile.s = "C:\Test.zip"
file.s = "C:\Temp\contact.txt"
file2.s = "C:\Temp\Info.txt"
outputfile.s = "C:\unzip file\"
If PureZIP_Archive_Create(zipfile, #APPEND_STATUS_CREATE)
   PureZIP_SetArchivePassword("password") 
   PureZIP_Archive_Compress(file, #False)
   PureZIP_Archive_Compress(file2,#False)
   PureZIP_Archive_Close()
 MessageRequester("ZIP/UNZIP","successful !",0)
EndIf
now how can i extract these file ?
like this ? no this not work

Code: Select all

MessageRequester("ZIP/UNZIP","Ready to unzip encryted file !",0)
If PureZIP_Archive_Read(zipfile)
 ;PureZIP_SetArchivePassword("")
 ReturnValue.l = PureZIP_Archive_FindFirst() = #UNZ_OK
  While ReturnValue = #UNZ_OK
   PureZIP_Archive_Extract(MyZIPOutPut.s, #True)
   ReturnValue = PureZIP_Archive_FindNext()
  Wend
  PureZIP_Archive_Close()
EndIf
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: extract encrypt zip file

Post by gnozal »

Micko wrote:now how can i extract these file ?
like this ? no this not work
This works :

Code: Select all

zipfile.s = "C:\Test.zip" 
outputfile.s = "C:\unzip file"

MessageRequester("ZIP/UNZIP","Ready to unzip encryted file !",0) 
If PureZIP_Archive_Read(zipfile) 
  PureZIP_SetArchivePassword("password") 
  ReturnValue.l = PureZIP_Archive_FindFirst() = #UNZ_OK 
  While ReturnValue = #UNZ_OK 
    PureZIP_Archive_Extract(outputfile, #True) 
    ReturnValue = PureZIP_Archive_FindNext() 
  Wend 
  PureZIP_Archive_Close() 
EndIf
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
X0r
Enthusiast
Enthusiast
Posts: 138
Joined: Tue May 01, 2007 3:49 am
Location: Germany

Post by X0r »

Is it possible to set a compressionmethod(fastest,...normal,....,best)?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Forge wrote:Is it possible to set a compressionmethod(fastest,...normal,....,best)?
You can use PureZIP_SetCompressionAlgorithm(Algorithm.l)
where Algorithm is :
#Z_NO_COMPRESSION : no compression (store)
#Z_DEFLATED : deflate algorithm (default)
Note that if #Z_DEFLATED is used, the compression is always set to #Z_BEST_COMPRESSION.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
AL90
Enthusiast
Enthusiast
Posts: 217
Joined: Fri Sep 16, 2005 7:47 pm
Location: Germany
Contact:

Post by AL90 »

Hi gnozal,

I have found a (possible?) Bug as I test my new Backup Software.
When I Backup a Folder with 5GB+ the archive will be corrupt.
I thought first I have a bug in my software, but later I see
the same problem if I use another software with PureZIP support.

Do you can confirm the bug ? or are archives limitet to 2GB ?
The Test-Archive is 2.08GB compressed.

BTW:
I use PureZIP_Archive_Compress()
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

AL90 wrote:Hi gnozal,
Do you can confirm the bug ? or are archives limitet to 2GB ?
The Test-Archive is 2.08GB compressed.
ZLIB should handle files up to 4 Gbytes, but PB longs are signed [0 - 2 Gbytes], so this may explain a problem with files > 2Gbytes.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
AL90
Enthusiast
Enthusiast
Posts: 217
Joined: Fri Sep 16, 2005 7:47 pm
Location: Germany
Contact:

Post by AL90 »

Is it possible to use quads to fix and set the limit up to 4GB ?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

AL90 wrote:Is it possible to use quads to fix and set the limit up to 4GB ?
Maybe, at least for the PB4 version.
Will see. But I don't have >2GB free on any of my PC's for the moment to test this.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
AL90
Enthusiast
Enthusiast
Posts: 217
Joined: Fri Sep 16, 2005 7:47 pm
Location: Germany
Contact:

Post by AL90 »

gnozal wrote:Maybe, at least for the PB4 version.
Superb. Thanks you very much. :wink:
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

AL90 wrote:
gnozal wrote:Maybe, at least for the PB4 version.
Superb. Thanks you very much. :wink:
Don't rejoice too soon :wink:
Some variables in the library (like filesize) are already quads.
The compression / decompression functions use a fixed size buffer, so no overflow problem expected here (size is 102400 bytes).

Code: Select all

        Stream = ReadFile(#PB_Any, FileName)
        If Stream
          *Buffer = AllocateMemory(#FileBufferSize)
          If *Buffer
            Repeat
              length = ReadData(Stream, *Buffer, #FileBufferSize)
              ReturnValue = WriteInZip(PureZIP_Handle_ZIP, *Buffer, length)
            Until Eof(Stream)
            FreeMemory(*Buffer)
          EndIf
          CloseFile(Stream)
        EndIf
Both use PB file functions, like ReadData() and WriteData(). Do they handle > 2GB files correctly ?

Some questions to help find the problem :
When you compress a file > 2GB,
1. Does PureZIP_Archive_Compress() fail (i.e. produce a corrupt archive : test with WinRAR or else) ?
2. Does PureZIP_Archive_Extract() fail (i.e. produce a corrupt extracted file) ?
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
AL90
Enthusiast
Enthusiast
Posts: 217
Joined: Fri Sep 16, 2005 7:47 pm
Location: Germany
Contact:

Post by AL90 »

1. Does PureZIP_Archive_Compress() fail (i.e. produce a corrupt archive : test with WinRAR or else) ?
No. PureZIP_Archive_Compress() was not failed.
But PureZIP_Archive_Read() will fail. So I wondered
why I get an empty (Linked) list of contains and have seen that
PureZIP_Archive_Read() has fail. I have test the archive with
TotalCMD and the directory will open. But some files in it are
corrupt as I try to extract in a temp directory.
2. Does PureZIP_Archive_Extract() fail (i.e. produce a corrupt extracted file) ?
Yes. See above.


Here now the part of my code to create the archive:

Code: Select all

  SetCurrentDirectory(Source$)
  If PureZIP_Archive_Create(ZipFile$, #APPEND_STATUS_CREATE)
    ResetList(Files())
    For i=0 To anzahl-1
      NumFile=i*100
      NextElement(Files())
      file$=Right(Files(),Len(Files())-Len(Source$))
      st=PureZIP_Archive_Compress(file$, #True)
      If st<>#Z_OK And ArcBreak=#False
        If Right(file$,1)<>"\" And FileSize(file$)>0
          MessageRequester("Info", "An error has occurred. can't create backup.", #MB_OK|#MB_ICONWARNING)
          ArcBreak=#True
        EndIf
      EndIf
      If ArcBreak=#True : Break : EndIf
    Next
    PureZIP_Archive_Close()
  EndIf
Both use PB file functions, like ReadData() and WriteData(). Do they handle > 2GB files correctly ?
I will test it and give you my result if it's done.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

AL90 wrote:
1. Does PureZIP_Archive_Compress() fail (i.e. produce a corrupt archive : test with WinRAR or else) ?
PureZIP_Archive_Read() has fail. I have test the archive with
TotalCMD and the directory will open. But some files in it are
corrupt as I try to extract in a temp directory.
Ok, so PureZIP_Archive_Compress() created a corrupt archive.
But there is not much PB code that could fail :

Code: Select all

        Stream = ReadFile(#PB_Any, FileName) 
        If Stream 
          *Buffer = AllocateMemory(#FileBufferSize) 
          If *Buffer 
            Repeat 
              length = ReadData(Stream, *Buffer, #FileBufferSize) 
              ReturnValue = WriteInZip(PureZIP_Handle_ZIP, *Buffer, length) 
            Until Eof(Stream) 
            FreeMemory(*Buffer) 
          EndIf 
          CloseFile(Stream) 
        EndIf
Maybe a ZLIB problem ?
AL90 wrote:
Both use PB file functions, like ReadData() and WriteData(). Do they handle > 2GB files correctly ?
I will test it and give you my result if it's done.
Thanks
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
AL90
Enthusiast
Enthusiast
Posts: 217
Joined: Fri Sep 16, 2005 7:47 pm
Location: Germany
Contact:

Post by AL90 »

gnozal wrote:Ok, so PureZIP_Archive_Compress() created a corrupt archive.
yes.
Maybe a ZLIB problem ?
That's possible. I can't say it exact.

So I have now test it with a 4GB file and the test was successful.
Here the testcode.

Code: Select all


; This Code will generate a 4GB TestFile and checks it with a small CRC32-test

TestFile$ = "C:\TestFile.tmp"
TotalSize.q = 4294963200
Size.q = 0
BlockSize.l = 102400 ; Test with '100 KB' BlockSize
Buffer.l = AllocateMemory(BlockSize)
FillMemory_(Buffer, BlockSize, $FF)
CRC32.l = CRC32Fingerprint(Buffer, BlockSize)

If Buffer

  If CreateFile(0,TestFile$)
    For i=1 To 41943
      WriteData(0, Buffer, BlockSize)
    Next
    CloseFile(0)
  EndIf

  If ReadFile(0, TestFile$)
    Repeat
      Size.q + BlockSize
      FillMemory_(Buffer, BlockSize, $00)
      x = ReadData(0, Buffer, BlockSize)
      If x <> BlockSize
        MessageRequester("Info", "Read Error!", #MB_OK|#MB_ICONWARNING)
        Break
      EndIf
      If CRC32Fingerprint(Buffer, BlockSize) <> CRC32
        MessageRequester("Info", "CRC32 Error!", #MB_OK|#MB_ICONWARNING)
        Break
      EndIf
    Until Eof(0)
    CloseFile(0)
  EndIf

  FreeMemory(Buffer)

EndIf

If Size = TotalSize
  MessageRequester("Info", "Test successfully completed!", #MB_OK|#MB_ICONINFORMATION)
EndIf
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

AL90 wrote:So I have now test it with a 4GB file and the test was successful.
Ok, it's almost the same code as I use [see post above] (except "ReturnValue = WriteInZip(PureZIP_Handle_ZIP, *Buffer, length)" of course).
So it seems to be a ZLIB issue ?
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply