PureLZMA : compress/uncompress data using LZMA algo
Posted: Mon Jan 05, 2004 4:51 pm
PureLZMA library
PureLZMA packer library
Overview
Functions
Basic -> PB4.xx : http://gnozal.ucoz.com/PureLZMA_.htm
Archiver -> PB4.3x+ : http://gnozal.ucoz.com/PureLZMA-Packer.htm
Archiver - Mem -> PB4.3x+ : http://gnozal.ucoz.com/PureLZMA-Packer-Mem.htm
Example 1
Example 2
Download
Only available for Purebasic Windows x86
PB4.0x-4.2x : http://gnozal.ucoz.com/PureLZMA_.zip
PB4.3x : http://gnozal.ucoz.com/PureLZMA_430.zip
PB4.4x : http://gnozal.ucoz.com/PureLZMA_440.zip
PB4.5x : http://gnozal.ucoz.com/PureLZMA_450.zip
PB4.6x : http://gnozal.ucoz.com/PureLZMA_460.zip
PB5.0x : http://gnozal.ucoz.com/PureLZMA_500.zip
Shared library (DLL) : http://gnozal.ucoz.com/PureLZMA_DLL.zip
Note : although the library uses the LZMA algorithm, which is one of many supported by 7-ZIP, it does no provide support for 7-ZIP archives.
PureLZMA packer library
Overview
Code: Select all
PureLZMA provides two functions to compress/uncompress data using the LZMA algorithm.
Optional RC4 based encryption is supported.
PureLZMA-Packer provides additional archiver functions.
It uses unmodified code from the LZMA SDK v4.62.
Basic -> PB4.xx : http://gnozal.ucoz.com/PureLZMA_.htm
Archiver -> PB4.3x+ : http://gnozal.ucoz.com/PureLZMA-Packer.htm
Archiver - Mem -> PB4.3x+ : http://gnozal.ucoz.com/PureLZMA-Packer-Mem.htm
Example 1
Code: Select all
TestFile$ = #PB_Compiler_Home + "PureBasic.exe"
;
Debug "----------- COMPRESS"
;
;
; Load original file into buffer
Stream = ReadFile(#PB_Any, TestFile$)
If Stream
BufferLen = Lof(Stream)
*Buffer = AllocateMemory(BufferLen)
If *Buffer = 0
End
EndIf
ReadData(Stream, *Buffer, BufferLen)
CloseFile(Stream)
Else
Debug "Could not read original file"
End
EndIf
;
;
; Compress buffer with LZMA
*OutBuffer = PureLZMA_Compress(*Buffer, @BufferLen)
Debug "PureLZMA_Compress : Returned value = " + Str(*OutBuffer)
Debug "PureLZMA_Compress : BufferLen = " + Str(BufferLen)
;
; PureLZMA_Compress() error codes :
; -1 : Memory allocation error
; -2 : Output buffer overflow or buffer not compressable
; -3 : Other compression error
;
FreeMemory(*Buffer)
;
;
; Save compressed buffer in '.lzma' file
If *OutBuffer > 0
Stream = CreateFile(#PB_Any, TestFile$ + ".lzma")
If Stream
WriteData(Stream, *OutBuffer, BufferLen)
CloseFile(Stream)
EndIf
PureLZMA_FreeBuffer(*OutBuffer)
Else
Debug "PureLZMA_Compress() failed, error = " + Str(*OutBuffer)
End
EndIf
;
;
;
Debug "----------- UNCOMPRESS"
;
;
; Load compressed buffer from '.lzma' file
Stream = ReadFile(#PB_Any, TestFile$ + ".lzma")
If Stream
BufferLen = Lof(Stream)
*Buffer = AllocateMemory(BufferLen)
If *Buffer = 0
End
EndIf
ReadData(Stream, *Buffer, BufferLen)
CloseFile(Stream)
Else
Debug "Could not read compressed file"
End
EndIf
;
;
; Unpack buffer with LZMA
*OutBuffer = PureLZMA_UnCompress(*Buffer, @BufferLen)
Debug "PureLZMA_UnCompress : Returned value = " + Str(*OutBuffer)
Debug "PureLZMA_UnCompress : BufferLen = " + Str(BufferLen)
;
; PureLZMA_UnCompress() error codes :
; 0 : Invalid data [not compressed by PureLZMA_Compress() ?]
; -1 : Memory allocation error
; -2 : Input buffer insufficient
; -3,-4 : Other compression error
;
FreeMemory(*Buffer)
;
;
; Save unpacked buffer in '.lzma.unpacked' file
If *OutBuffer > 0
Stream = CreateFile(#PB_Any, TestFile$ + ".lzma.unpacked")
If Stream
WriteData(Stream,*OutBuffer, BufferLen)
CloseFile(Stream)
EndIf
PureLZMA_FreeBuffer(*OutBuffer)
Else
Debug "PureLZMA_UnCompress() failed, error = " + Str(*OutBuffer)
End
EndIf
;
; Compare original and '.lzma.unpacked' files !
;
Debug "----------- RESULT"
If MD5FileFingerprint(TestFile$) = MD5FileFingerprint(TestFile$ + ".lzma.unpacked")
Debug "Compression with PureLZMA is Ok !"
Else
Debug "PureLZMA failed !"
EndIf
Code: Select all
If PureLZMA_Archive_Create(#PB_Compiler_Home + "\Examples\PureLZMA\TestArchive.arc")
PureLZMA_Archive_Compress(#PB_Compiler_Home + "Examples\Sources\Screen3DRequester.pb", 0)
PureLZMA_Archive_Compress(#PB_Compiler_Home + "Examples\Sources\MDI ImageViewer.pb", 0)
PureLZMA_Archive_Compress(#PB_Compiler_Home + "Examples\Sources\Data\Background.bmp", 0)
PureLZMA_Archive_Close()
EndIf
;
If PureLZMA_Archive_Read(#PB_Compiler_Home + "\Examples\PureLZMA\TestArchive.arc")
ArchiveInfo.LZMA_ArchiveInfo
Status = PureLZMA_Archive_FindFirst()
;
While Status
;
If PureLZMA_Archive_GetArchiveInfo(@ArchiveInfo)
Debug "----------------"
Debug "CRCUnpacked = $" + RSet(Hex(ArchiveInfo\CRCUnpacked, #PB_Long), 8, "0")
Debug "CRCPacked = $" + RSet(Hex(ArchiveInfo\CRCPacked, #PB_Long), 8, "0")
Debug "CRCPacked CALC = $" + RSet(Hex(PureLZMA_Archive_GetCRC(), #PB_Long), 8, "0")
Debug "LengthUnpacked = " + Str(ArchiveInfo\LengthUnpacked)
Debug "LengthPacked = " + Str(ArchiveInfo\LengthPacked) + " (" + StrF(ArchiveInfo\LengthPacked / ArchiveInfo\LengthUnpacked * 100, 2) + "% of original)"
Debug "LZMAFlags = " + Str(ArchiveInfo\LZMAFlags)
Debug "FileAttributes = $" + Hex(ArchiveInfo\FileAttributes)
Debug "Filename = '" + ArchiveInfo\Filename + "'"
EndIf
;
Status = PureLZMA_Archive_FindNext()
;
Wend
;
PureLZMA_Archive_Close()
EndIf
Only available for Purebasic Windows x86
PB4.0x-4.2x : http://gnozal.ucoz.com/PureLZMA_.zip
PB4.3x : http://gnozal.ucoz.com/PureLZMA_430.zip
PB4.4x : http://gnozal.ucoz.com/PureLZMA_440.zip
PB4.5x : http://gnozal.ucoz.com/PureLZMA_450.zip
PB4.6x : http://gnozal.ucoz.com/PureLZMA_460.zip
PB5.0x : http://gnozal.ucoz.com/PureLZMA_500.zip
Shared library (DLL) : http://gnozal.ucoz.com/PureLZMA_DLL.zip
Note : although the library uses the LZMA algorithm, which is one of many supported by 7-ZIP, it does no provide support for 7-ZIP archives.