Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

Decompress Large Text File in Blocks

See more Compression Examples

Decompresses a large text file in blocks, and compares the restored (decompressed) file with the original to make sure it's correct.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFileAccess.pb"
IncludeFile "CkCompression.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

    ;  First, let's compress a text file.
    ;  We'll then decompress in blocks, and compare the decompressed with the original file.

    ;  Compress a text file:
    compress.i = CkCompression::ckCreate()
    If compress.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCompression::setCkAlgorithm(compress, "deflate")

    success = CkCompression::ckCompressFile(compress,"qa_data/hamlet.xml","qa_data/hamlet_compressed.dat")
    If success = 0
        Debug CkCompression::ckLastErrorText(compress)
        CkCompression::ckDispose(compress)
        ProcedureReturn
    EndIf

    fac.i = CkFileAccess::ckCreate()
    If fac.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Examine the uncompressed and compressed sizes:
    originalPath.s = "qa_data/hamlet.xml"
    ;  Note: The FileSize method returns a signed 32-bit integer.  If the file is potentially larger than 2GB, call FileSizeStr instead to return
    ;  the size of the file as a string, then convert to an integer value.
    Debug "uncompressed size: " + Str(CkFileAccess::ckFileSize(fac,originalPath))
    Debug "compressed size: " + Str(CkFileAccess::ckFileSize(fac,"qa_data/hamlet_compressed.dat"))

    ;  Decompress in blocks..
    facSrc.i = CkFileAccess::ckCreate()
    If facSrc.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    facDest.i = CkFileAccess::ckCreate()
    If facDest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFileAccess::ckOpenForRead(facSrc,"qa_data/hamlet_compressed.dat")

    ;  If we compress in 32K chunks, find out how many blocks there will be.
    blockSize.i = 32768
    numBlocks.i = CkFileAccess::ckGetNumBlocks(facSrc,blockSize)

    ;  Open an output file for the decompressed data.
    restoredPath.s = "qa_output/hamlet_restored.xml"
    success = CkFileAccess::ckOpenForWrite(facDest,restoredPath)
    If success = 0
        Debug CkFileAccess::ckLastErrorText(facDest)
        CkCompression::ckDispose(compress)
        CkFileAccess::ckDispose(fac)
        CkFileAccess::ckDispose(facSrc)
        CkFileAccess::ckDispose(facDest)
        ProcedureReturn
    EndIf

    decompressedStr.s
ERROR - PureBasic is an array language

    ;  Assuming numBlocks > 1
    CkCompression::setCkFirstChunk(compress, 1)
    CkCompression::setCkLastChunk(compress, 0)

    i.i = 0
    While i < numBlocks
        compressedBytes = CkFileAccess::ckReadBlock(facSrc,i,blockSize)
        decompressedStr = CkCompression::ckDecompressString(compress,compressedBytes)

        CkFileAccess::ckAppendText(facDest,decompressedStr,"utf-8")

        i = i + 1

        CkCompression::setCkFirstChunk(compress, 0)
        If i = (numBlocks - 1)
            CkCompression::setCkLastChunk(compress, 1)
        EndIf

    Wend

    CkFileAccess::ckFileClose(facSrc)
    CkFileAccess::ckFileClose(facDest)

    ;  Examine the size of the restored file.
    Debug "restored size: " + Str(CkFileAccess::ckFileSize(fac,restoredPath))

    ;  Compare the contents of the original with the restored.
    bEqualContents.i = CkFileAccess::ckFileContentsEqual(fac,restoredPath,originalPath)
    Debug "Contents Equal: " + Str(bEqualContents)


    CkCompression::ckDispose(compress)
    CkFileAccess::ckDispose(fac)
    CkFileAccess::ckDispose(facSrc)
    CkFileAccess::ckDispose(facDest)


    ProcedureReturn
EndProcedure