;
; ------------------------------------------------------------
;
;   PureBasic - Compressor example file
;
;    (c) 2001 - Fantaisie Software
;
; ------------------------------------------------------------
;

If ReadFile(0, OpenFileRequester("Choose a file to compress", "", "*.*", 0))
  FileLength = Lof(0)
  
  ; Allocate the 2 memory buffers needed for compression..
  ;
  *Source = AllocateMemory(FileLength)
  *Target = AllocateMemory(FileLength+8)
  If FileLength And *Source And *Target
    ReadData(0, *Source, FileLength) ; Read the whole file in the memory buffer
    
    ; Compress our file, which is in memory (and use a timer to see the time spend by compression..)
    ;
    CompressedLength = PackMemory(*Source, *Target, FileLength)
    If CompressedLength

      DecompressedLength = UnpackMemory(*Target, *Source)
      If DecompressedLength = FileLength
        MessageRequester("Info", "De/Compression succeded:"+#LF$+#LF$+"Old size: "+Str(FileLength)+#LF$+"New size: "+Str(CompressedLength))
      EndIf
    Else
      MessageRequester("Error", "Can't compress the file")
    EndIf 
    
    FreeMemory(*Source)
    FreeMemory(*Target)    
    
  EndIf
  
  CloseFile(0)
EndIf

End