
The library was compiled with Visual C++ 2010 Beta 2 with maximum optimizations.
http://3dfolio.com/files/7z.zip
Code: Select all
#LZMA_PROPS_SIZE=5
#LZMA_SIZE_OFFSET=#LZMA_PROPS_SIZE
#LZMA_HEADER_SIZE=#LZMA_SIZE_OFFSET+8
#SZ_OK=0
#SZ_ERROR_DATA=1
#SZ_ERROR_MEM=2
#SZ_ERROR_CRC=3
#SZ_ERROR_UNSUPPORTED=4
#SZ_ERROR_PARAM=5
#SZ_ERROR_INPUT_EOF=6
#SZ_ERROR_OUTPUT_EOF=7
#SZ_ERROR_READ=8
#SZ_ERROR_WRITE=9
#SZ_ERROR_PROGRESS=10
#SZ_ERROR_FAIL=11
#SZ_ERROR_THREAD=12
#SZ_ERROR_ARCHIVE=16
#SZ_ERROR_NO_ARCHIVE=17
Procedure _LzmaCompress(*Dest, *DestLen, *Src, SrcLen, *OutProps, *OutPropsSize, Level=5, DictSize=((1<<24)&$FFFFFFFF), Lc=3, Lp=0, Pb=2, Fb=32, NumThreads=2)
Protected Lib, Ptr, Result=#SZ_OK
Lib=OpenLibrary(#PB_Any,"7z.dll")
Ptr=GetFunction(Lib,"LzmaCompress")
If Ptr
Result=CallFunctionFast(Ptr,*Dest,*DestLen,*Src,SrcLen,*OutProps,*OutPropsSize,Level,DictSize,Lc,Lp,Pb,Fb,NumThreads)
EndIf
CloseLibrary(Lib)
ProcedureReturn Result
EndProcedure
Procedure _LzmaUncompress(*Dest, *DestLen, *Src, *SrcLen, *Props, PropsSize)
Protected Lib, Ptr, Result=#SZ_OK
Lib=OpenLibrary(#PB_Any,"7z.dll")
Ptr=GetFunction(Lib,"LzmaUncompress")
If Ptr
Result=CallFunctionFast(Ptr,*Dest,*DestLen,*Src,*SrcLen,*Props,PropsSize)
EndIf
CloseLibrary(Lib)
ProcedureReturn Result
EndProcedure
Procedure LzmaCompressFile(SourceFile.s, DestinationFile.s, Level=5, DictSize=((1<<24)&$FFFFFFFF))
Protected FileID, SourceSize, *SourceMem, DestSize, *DestMem, *OutProps, OutPropsSize, Result
FileID=ReadFile(#PB_Any,SourceFile.s)
If Not FileID
ProcedureReturn #SZ_ERROR_READ
EndIf
SourceSize=Lof(FileID)
If SourceSize=0
CloseFile(FileID)
ProcedureReturn #SZ_ERROR_READ
EndIf
;/ Allocate memory for the source file to be read into memory
*SourceMem=AllocateMemory(SourceSize)
If Not *SourceMem
CloseFile(FileID)
ProcedureReturn #SZ_ERROR_MEM
EndIf
ReadData(FileID,*SourceMem,SourceSize)
CloseFile(FileID)
;/ Allocate 105% of file size for destination buffer + 64 kilobytes
DestSize=SourceSize/20*21+(1<<16)
*DestMem=AllocateMemory(DestSize)
If Not *DestMem
FreeMemory(*SourceMem)
ProcedureReturn #SZ_ERROR_MEM
EndIf
;/ Allocate memory for the properties section
*OutProps=AllocateMemory(#LZMA_PROPS_SIZE)
If Not *OutProps
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
ProcedureReturn #SZ_ERROR_MEM
EndIf
OutPropsSize=#LZMA_PROPS_SIZE
Result=_LzmaCompress(*DestMem,@DestSize,*SourceMem,SourceSize,*OutProps,@outPropsSize,Level,DictSize)
If Not Result=#SZ_OK Or Not DestSize Or Not OutPropsSize
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
FreeMemory(*OutProps)
ProcedureReturn Result
EndIf
FileID=CreateFile(#PB_Any,DestinationFile.s)
If Not FileID
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
FreeMemory(*OutProps)
ProcedureReturn #SZ_ERROR_WRITE
EndIf
WriteData(FileID,*OutProps,#LZMA_PROPS_SIZE)
WriteQuad(FileID,SourceSize)
WriteData(FileID,*DestMem,DestSize)
CloseFile(FileID)
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
FreeMemory(*OutProps)
ProcedureReturn Result
EndProcedure
Procedure LzmaUncompressFile(SourceFile.s, DestinationFile.s)
Protected FileID, SourceSize, *OutProps, DictSize, *SourceMem, *DestMem, DestSize, Result
FileID=ReadFile(#PB_Any,SourceFile.s)
If Not FileID
ProcedureReturn #SZ_ERROR_READ
EndIf
SourceSize=Lof(FileID)
If SourceSize=0
CloseFile(FileID)
ProcedureReturn #SZ_ERROR_READ
EndIf
;/ Read header information
*OutProps=AllocateMemory(#LZMA_PROPS_SIZE)
ReadData(FileID,*OutProps,#LZMA_PROPS_SIZE)
DestSize=ReadQuad(FileID)
DictSize=PeekL(*OutProps+1)
;/ Read compressed data to memory
SourceSize=Lof(FileID)-Loc(FileID)
*SourceMem=AllocateMemory(SourceSize)
If Not *SourceMem
FreeMemory(*OutProps)
CloseFile(FileID)
ProcedureReturn #SZ_ERROR_MEM
EndIf
ReadData(FileID,*SourceMem,SourceSize)
CloseFile(FileID)
;/ Allocate memory to hold the uncompressed data
*DestMem=AllocateMemory(DestSize)
If Not *DestMem
FreeMemory(*OutProps)
FreeMemory(*SourceMem)
ProcedureReturn #SZ_ERROR_MEM
EndIf
Result=_LzmaUncompress(*DestMem,@DestSize,*SourceMem,@SourceSize,*OutProps,#LZMA_PROPS_SIZE)
If Not Result=#SZ_OK Or Not DestSize Or Not SourceSize
FreeMemory(*OutProps)
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
ProcedureReturn Result
EndIf
FileID=CreateFile(#PB_Any,DestinationFile.s)
If Not FileID
FreeMemory(*OutProps)
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
ProcedureReturn #SZ_ERROR_WRITE
EndIf
WriteData(FileID,*DestMem,DestSize)
CloseFile(FileID)
FreeMemory(*OutProps)
FreeMemory(*SourceMem)
FreeMemory(*DestMem)
ProcedureReturn Result
EndProcedure
Code: Select all
Procedure CompressionTest(FileName.s, Level)
Protected Start
Start=ElapsedMilliseconds()
If Not LzmaCompressFile(FileName.s,"compressed",Level,((1<<24)&$FFFFFFFF))=#SZ_OK
Debug "Error compressing"
End
EndIf
Debug "Compression time "+Str(ElapsedMilliseconds()-Start)
Start=ElapsedMilliseconds()
If Not LzmaUnCompressFile("compressed","uncompressed")=#SZ_OK
Debug "Error decompressing"
End
EndIf
Debug "Decompression time "+Str(ElapsedMilliseconds()-Start)
Debug "Compressed size "+StrF(FileSize("compressed")/FileSize("uncompressed")*100,2)
EndProcedure
Code: Select all
Level 1
Compression time 1935
Decompression time 234
Compressed ratio 18.72%
Level 5
Compression time 4431
Decompression time 234
Compressed ratio 17.30%
Level 9
Compression time 4555
Decompression time 249
Compressed ratio 17.30%
After compression: 1.16 MB
I was also considering bzip2 but it couldn't even manage to compress the same file to under 2 MB. LZMA is supposed to be faster at decompressing as well. Though it's much slower at compression.