Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'dart:typed_data';
import 'package:chilkat/chilkat.dart';
void main() {
// 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:
final compress = CkCompression();
compress.algorithm = 'deflate';
try {
compress.compressFile('qa_data/hamlet.xml', 'qa_data/hamlet_compressed.dat');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final fac = CkFileAccess();
// Examine the uncompressed and compressed sizes:
final originalPath = '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.
print('uncompressed size: ${fac.fileSize(originalPath)}');
print('compressed size: ${fac.fileSize('qa_data/hamlet_compressed.dat')}');
// Decompress in blocks..
final facSrc = CkFileAccess();
final facDest = CkFileAccess();
facSrc.openForRead('qa_data/hamlet_compressed.dat');
// If we compress in 32K chunks, find out how many blocks there will be.
final blockSize = 32768;
final numBlocks = facSrc.getNumBlocks(blockSize);
// Open an output file for the decompressed data.
final restoredPath = 'qa_output/hamlet_restored.xml';
try {
facDest.openForWrite(restoredPath);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
var decompressedStr = '';
var compressedBytes = Uint8List(0);
// Assuming numBlocks > 1
compress.firstChunk = true;
compress.lastChunk = false;
var i = 0;
while (i < numBlocks) {
compressedBytes = facSrc.readBlock(i, blockSize);
decompressedStr = compress.decompressString(compressedBytes);
facDest.appendText(decompressedStr, 'utf-8');
i++;
compress.firstChunk = false;
if (i == (numBlocks - 1)) {
compress.lastChunk = true;
}
}
facSrc.fileClose();
facDest.fileClose();
// Examine the size of the restored file.
print('restored size: ${fac.fileSize(restoredPath)}');
// Compare the contents of the original with the restored.
final bEqualContents = fac.fileContentsEqual(restoredPath, originalPath);
print('Contents Equal: $bEqualContents');
}