Qt LZO examples


Table Of Contents


In QtLzo/examples/lzotool, there is an example code.


Compress using only memory data blocks

Compress using only memory data blocks is frequently used on network data exchange applications.

Compress original completed source data into LZO compressed format


Using default parameters

Using a single function
QByteArray sourceData ;
QByteArray lzoData ;
// ...
// Load sourceData by your own code
// ...
if ( ToLZO ( sourceData , lzoData ) ) {
  // handling LZO compressed data
} else {
  // error when compress data into LZO format
}

Using a QtLzo class object

QByteArray   sourceData                   ;
QByteArray lzoData ;
QtLzo        L                            ;
int
r ;
QVariantList v                            ;
v << level                                ;
v << method                               ;
v << blocks ;
v << outputLength ;
r = L . BeginCompress ( v )               ;
if ( L . IsCorrect ( r ) )                {
  L . doCompress ( sourceData , lzoData ) ;
}                                         ;


Compress unknown length source data blocks into LZO format in multiple processes


QByteArray   sourceData                      ;
QByteArray lzoData ;
QByteArray Compressed ;
QtLzo        L                               ;
int
r ;
QVariantList v                               ;
v << level                                   ; // default 9
v << method                                  ; // default LZO1x
v << blocks ; // default 8192
v << outputLength ; // default 0
r = L . BeginCompress ( v )                  ;
while ( MyOwnObtainData ( sourceData ) ) {
  r = L . doSection ( sourceData , lzoData ) ;
if ( L . IsCorrect ( r ) ) { if ( lzoData . size ( ) > 0 ) {
Compressed . append ( lzoData ) ; // or handling the lzoData by your own function
} ;
} ;
} ;
L . CompressDone ( Compressed ) ;


Decompress using only memory data blocks


Decompress a single completed LZO format data into a completed source data


Using a single function
QByteArray sourceData ;
QByteArray lzoData ;
// ...
// Load lzoData by your own code
// ...
if ( FromLZO ( lzoData , sourceData ) ) {
  // handling sourceData
} else {
  // error when decompress LZO data into sourceData
}

Using a QtLzo class object
QByteArray sourceData                       ;
QByteArray lzoData                          ;
QtLzo      L                                ;
int        r                                ;
r = L . BeginDecompress ( )                 ;
if ( L . IsCorrect ( r ) )                  {
  L . doDecompress ( lzoData , sourceData ) ;
}                                           ;

Decompress unknown length compressed LZO data into original source data in multiple processes


QByteArray sourceData                          ;
QByteArray lzoData                             ;
QByteArray Decompressed ; QtLzo L ;
int        r                                   ;
r = L . BeginDecompress ( )                    ;
while ( MyOwnObtainLZO ( lzoData ) ) {
  r = L . undoSection ( lzoData , sourceData ) ;
  if ( L . IsCorrect ( r ) )                   {
if ( sourceData . size ( ) > 0 ) {
Decompressed . append ( sourceData ) ;
    }                                          ;
  }                                            ;
}                                              ;


Compress memory data block into LZO file format


QByteArray sourceData ;
// ...
// Obtain sourceData by your own code
// ...
if ( SaveLZO ( "LzoFilename.lzo" , sourceData ) ) {
  // successful compress sourceData into LzoFilename.lzo file
} else {
  // error to compress into LZO file
}


Decompress LZO file format into memory data block


QByteArray sourceData ;

if ( LoadLZO ( "LzoFilename.lzo" , sourceData ) ) {
  // successful decompress LzoFilename.lzo file into sourceData
} else {
  // error to decompress LZO file
}


Decompress LZO file format into normal file


if ( LzoToFile ( "LzoFilename.lzo" , "NormalFilename.dat" ) ) {
  // successful decompress LzoFilename.lzo file into NormalFilename.dat
} else {
  // error to decompress LZO file
}


Compress normal file into LZO file format


if ( FileToLzo ( "NormalFilename.dat" , "LzoFilename.lzo" ) ) {
  // successful compress NormalFilename.dat into LzoFilename.lzo
} else {
  // error to compress into LZO file
}


Qt LZO via Javascript(Qt Script)


Using Qt Script to call ScriptableLzo


QScriptValue AttachmentLzo(QScriptContext * context,QScriptEngine * engine)
{
  ScriptableLzo * lzo = new ScriptableLzo ( engine ) ;
  return engine -> newQObject             ( lzo    ) ;
}

bool JsLzo(QString ifile,QString entry)
{
  QString    m                                                               ;
  QByteArray SCRIPT                                                          ;
  ////////////////////////////////////////////////////////////////////////////
  if ( ( ! LoadAll ( ifile , SCRIPT ) ) || ( SCRIPT . size ( ) <= 0 ) )      {
//
ifile can not be loaded
    return false                                                             ;
  }                                                                          ;
  ////////////////////////////////////////////////////////////////////////////
  QScriptEngine    Engine                                                    ;
  QString          script                                                    ;
  QScriptValue     func                                                      ;
  QScriptValue     global                                                    ;
  QScriptValue     lzo                                                       ;
  QScriptValue     again                                                     ;
  QScriptValueList args                                                      ;
  ////////////////////////////////////////////////////////////////////////////
  script = QString::fromUtf8     ( SCRIPT                                  ) ;
  func   = Engine . evaluate     ( script                                  ) ;
  global = Engine . globalObject (                                         ) ;
  global . setProperty ( "QtLZO"  , Engine . newFunction ( AttachmentLzo ) ) ;
  lzo    = global . property     ( entry                                   ) ;
  ////////////////////////////////////////////////////////////////////////////
  if ( lzo . isFunction ( ) )                                                {
    again = lzo . call ( func , args  )                                      ;
  } else                                                                     {
    // function entry can not be found in ifile.
    return false                                                             ;
  }                                                                          ;
  ////////////////////////////////////////////////////////////////////////////
  return true                                                                ;
}

LZO.js

function CompressLZO()
{
  lzo = new QtLZO() ;
  lzo . ToLzo ( "Testing.txt" , "Testing.lzo" , 9 , 1 ) ;
  delete lzo ;
}

function DecompressLZO()
{
  lzo = new QtLZO() ;
  lzo . ToFile ( "Testing.lzo" , "Testing.txt" ) ;
  delete lzo ;
}


Neutrino International Inc. 2001~2015