CompressFile Method  
 

Compress the contents of the specified file.

Syntax

object.CompressFile( InputFile, OutputFile, [CompressionType], [CompressionLevel] )

Parameters

InputFile
A string value that is the name of the file that will be compressed. The file must exist, and it must be a regular file that can be opened for reading by the current process. An error will be returned if a character device, such as the console, is specified as the file name.
OutputFile
A string value that is the name of the file that is to contain the compressed file data. If the file exists, it must be a regular file that can be opened for writing by the current process and will be overwritten. If the file does not exist, it will be created. An error will be returned if a character device, such as the console, is specified as the file name.
CompressionType
A numeric value which determines the algorithm that will be used to compress the data. One of the following values may be specified. If this argument is not specified, the Deflate algorithm is used.
Value Description
fileCompressionDeflate A compression algorithm that combines LZ77 algorithm for creating common substrings and Huffman coding to process the different frequencies of byte sequences in the data stream. Deflate is widely used by compression software and provides a good balance between the data compression ratio and system resources used. This is the default compression method.
fileCompressionBzip2 A compression algorithm that rearranges blocks of data in sorted order and then uses Huffman coding to process different frequencies of data within the block. Burrows-Wheeler compression provides a better compression ratio than the Deflate algorithm, however it requires more resources to perform the compression.
fileCompressionLzma A compression algorithm that combines the LZ77 algorithm for dictionary-based compression with range encoding to efficiently represent repetitive patterns in the data. LZMA can achieve higher compression ratios than Deflate or the Burrows-Wheeler algorithms; however, it requires more memory and CPU resources due to the larger dictionary sizes and more complex encoding algorithm.
CompressionLevel
A numeric value which specifies the compression level to use. A value of zero specifies that the default compression level appropriate for the selected algorithm should be used, balancing resource usage and the compression ratio of the data. A value of 1 specifies that the compression should be performed using minimal memory resources, at the expense of the compression ratio. The maximum value of 9 specifies that the algorithm should use more memory to achieve the maximum compression ratio. It is recommended that most applications use the default value of zero.

Return Value

This method returns a value of zero if the file was successfully compressed. A non-zero return value specifies an error code which indicates the reason for the failure.

Remarks

The CompressFile method compresses the contents of the specified file. The compression ratio achieved depends on the type of file being compressed. Note that the compressed file is not stored in an archive format that is recognized by third-party applications such as PKZip or WinZip.

If the output file already exists, it will be replaced with the compressed contents of the input file. If the compression fails for any reason, the output file will be deleted. If you want to ensure an existing output file will be preserved if an error occurs, you should create a temporary file and use it as the output file. If this method succeeds, then you can rename or copy the temporary file which contains the compressed data.

See Also

CompareFile Method, CompressData Method, DecodeFile Method, EncodeFile Method, ExpandFile Method