Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loCompress
LOCAL loFac
LOCAL lcOriginalPath
LOCAL loFacSrc
LOCAL loFacDest
LOCAL lnBlockSize
LOCAL lnNumBlocks
LOCAL lcRestoredPath
LOCAL lcDecompressedStr
LOCAL loCompressedBytes
LOCAL i
LOCAL lnBEqualContents

lnSuccess = 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:
loCompress = CreateObject('Chilkat.Compression')
loCompress.Algorithm = "deflate"

lnSuccess = loCompress.CompressFile("qa_data/hamlet.xml","qa_data/hamlet_compressed.dat")
IF (lnSuccess = 0) THEN
    ? loCompress.LastErrorText
    RELEASE loCompress
    CANCEL
ENDIF

loFac = CreateObject('Chilkat.FileAccess')

* Examine the uncompressed and compressed sizes:
lcOriginalPath = "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.
? "uncompressed size: " + STR(loFac.FileSize(lcOriginalPath))
? "compressed size: " + STR(loFac.FileSize("qa_data/hamlet_compressed.dat"))

* Decompress in blocks..
loFacSrc = CreateObject('Chilkat.FileAccess')
loFacDest = CreateObject('Chilkat.FileAccess')

loFacSrc.OpenForRead("qa_data/hamlet_compressed.dat")

* If we compress in 32K chunks, find out how many blocks there will be.
lnBlockSize = 32768
lnNumBlocks = loFacSrc.GetNumBlocks(lnBlockSize)

* Open an output file for the decompressed data.
lcRestoredPath = "qa_output/hamlet_restored.xml"
lnSuccess = loFacDest.OpenForWrite(lcRestoredPath)
IF (lnSuccess = 0) THEN
    ? loFacDest.LastErrorText
    RELEASE loCompress
    RELEASE loFac
    RELEASE loFacSrc
    RELEASE loFacDest
    CANCEL
ENDIF

* Assuming numBlocks > 1
loCompress.FirstChunk = 1
loCompress.LastChunk = 0

i = 0
DO WHILE i < lnNumBlocks
    loCompressedBytes = loFacSrc.ReadBlock(i,lnBlockSize)
    lcDecompressedStr = loCompress.DecompressString(loCompressedBytes)

    loFacDest.AppendText(lcDecompressedStr,"utf-8")

    i = i + 1

    loCompress.FirstChunk = 0
    IF (i = (lnNumBlocks - 1)) THEN
        loCompress.LastChunk = 1
    ENDIF

ENDDO

loFacSrc.FileClose()
loFacDest.FileClose()

* Examine the size of the restored file.
? "restored size: " + STR(loFac.FileSize(lcRestoredPath))

* Compare the contents of the original with the restored.
lnBEqualContents = loFac.FileContentsEqual(lcRestoredPath,lcOriginalPath)
? "Contents Equal: " + STR(lnBEqualContents)

RELEASE loCompress
RELEASE loFac
RELEASE loFacSrc
RELEASE loFacDest