Uncompress a file

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Uncompress a file

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Franco.

Code: Select all

; (c) 2002 - Franco's template - absolutely freeware
; UncompressFile - to be used with CompressFile() posted here:
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=3735
; Find out the unpacked file length of the packed file and uncompress it
; The Timer and the MessageBox is not needed for the main function...

UseZipPacker()

Procedure UncompressFile(file2Uncompress.s)
  If ReadFile(0, file2Uncompress)
    fileLength = Lof(0) - 4
    originalLength = ReadLong(0)
    *fileBuffer = AllocateMemory(fileLength)
    *uncompressBuffer = AllocateMemory(originalLength)
    If fileLength And *fileBuffer And *uncompressBuffer
      ReadData(0, *fileBuffer, fileLength)
      CloseFile(0)
      startTimer = GetTickCount_()
      uncompressedLength = UncompressMemory(*fileBuffer, fileLength,
                                            *uncompressBuffer, originalLength)
      If uncompressedLength = originalLength
        uncompressedFileName.s = SaveFileRequester("Choose a file to save to:",
                                                   "", "All files | *.*", 0)
        If uncompressedFileName
          OpenFile(1,uncompressedFileName)
          WriteData(1, *uncompressBuffer, uncompressedLength)
          CloseFile(1)
          uncompressionTime = GetTickCount_() - startTimer
          MessageRequester("Info", "Decompression succeded:" + Chr(10) + Chr(10) +
                                   "Old size: " + Str(fileLength) + Chr(10) +
                                   "New size:" + Str(uncompressedLength) + Chr(10) +
                                   "Decompression Time: " + Str(uncompressionTime) + " ms",
                                   #MB_ICONINFORMATION)
          ProcedureReturn 1
        EndIf
      EndIf      
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure

file2Uncompress.s = OpenFileRequester("Choose a file to uncompress",
                                      "", "All files | *.*", 0)
If file2Uncompress
  If UncompressFile(file2Uncompress) = 0
    MessageRequester("Info", "Something went wrong.", #MB_ICONINFORMATION)
  EndIf
EndIf
Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - franco on 02 May 2002 17:56:19