e-CryptIt Engine - Compression Xojo Plugin

LZMA.Compress Method

Compresses the data that has been set by the input source, to the output destination.

Compress() as Boolean

Parameters

Returns

Boolean
True if successful, else false.

Remarks

If both input and output have not been set then this function will return false.
If some error occurs during compression then this function will return false.

After compressing then Input and output targets will need to be set again before compressing or decompressing again.

Compressing file streams:

Dim compressor as LZMA
Dim sourceItem as FolderItem
Dim destItem as FolderItem

sourceItem = GetOpenFolderItem("*.*")

if sourceItem <> nil then
    destItem = sourceItem.Parent.Child(sourceItem.Name + ".lzma")
   
    if destitem.Exists then
       MsgBox "Destination file already exists"
       return
    end if
   
    compressor = new LZMA()
   
    compressor.SetInputToStream(BinaryStream.Open(sourceItem))
    compressor.SetOutputToStream(BinaryStream.Create(destItem))
   
    if not compressor.Compress() then
       MsgBox "Failed compressing"
    end if
end if


Compressing strings:
Dim compressor as LZMA
Dim sourceItem as FolderItem

compressor = new LZMA()

compressor.SetInputToString("This is some very very long test 1234567890 1234567890. ABCDEFGabcdefgh")
compressor.SetOutputToString()

if not compressor.Compress() then
    MsgBox "Failed!2"
end if

MsgBox "Number of compressed bytes: " + Str(compressor.OutputString.Len) // Do not try to display the compressed string since its binary data and not text

// Lets decompress and see what we get
compressor.SetInputToString(compressor.OutputString)
compressor.SetOutputToString()

if compressor.Decompress() then
    MsgBox compressor.OutputString.DefineEncoding(Encodings.UTF8) // We happened to know for sure that the string was UTF8 since strings defined in code in Xojo are UTF8
end if

See Also

LZMA Class