Table of Contents

28.1.1 Class Compress

For compressing a file or packing a single file into an archive, these 4 selected properties and the Compress.File method are of particular interest:

PropertyDescription
TypeBefore you can use any of the methods of this class, you have to select the compression driver you want to use. There are 2 to choose from: zlib or bzlib2
MinLowest compression level (Level=1) for the used compression driver
DefaultStandard compression level: zlib → 6 and bzlib2 → 9
MaxLargest compression level is equal to 9 for zlib and bzlib2

Table 28.1.1.1: Properties of the class Compress

The Compress.File method has the following syntax:

SUB File ( Source AS String, Target AS String [ , Level AS Integer ] )

Source: path to the file to be compressedTarget: path of the archive file. If the archive already exists, it is overwritten without prompting.Level: The specification of the compression level is optional. If this specification is missing, then the default compression level (Table 28.1.1.1) for the compression driver used is used.


Figure 28.1.1.1: Programme for packing a file according to two different algorithms.

28.1.1.1 Project Compress

For this project, the full source code is given and briefly explained in the comments below:

' Gambas class file
' Um eine komprimierte Datei zu generieren, benötigen Sie die Komponente gb.compress!
 
Private sFileSource As String
Private sFileTarget As String
Private sCompLevel As String
 
Private sExtension As String
 
Public Sub Form_Open()
  FMain.Center
  FMain.Resizable = False
  SpinBoxZLIB.MinValue = 1     ' Best speed
  SpinBoxZLIB.MaxValue = 9     ' Best compression
  SpinBoxZLIB.Value = 6        ' Default
  SpinBoxBZLIB2.MinValue = 1   ' Best speed - Fast
  SpinBoxBZLIB2.MaxValue = 9   ' Best compression
  SpinBoxBZLIB2.Value = 9      ' Default
End ' Form_Open()
 
Public Sub btnCompressZLIB_Click()
  Dim Cp As New Compress
 
  sExtension = ".gz"
 
  Dialog.Filter = ["*.csv", "CSV-File", "*", "All Files"]
  Dialog.Path = User.Home
  Dialog.Title = "Select a file!"
  If Dialog.OpenFile() Then Return
 
  sFileSource = Dialog.Path
  sFileTarget = sFileSource & sExtension
 
  Cp.Type = "zlib"
  Cp.File(sFileSource, sFileTarget, SpinBoxZLIB.Value)
 
End ' btnCompressZLIB_Click()
 
Public Sub btnCompressBZLIB2_Click()
  Dim Cp As New Compress
 
  sExtension = ".bz2"
 
  Dialog.Filter = ["*.csv", "CSV-File", "*", "All Files"]
  Dialog.Path = User.Home
  Dialog.Title = "Select a file!"
  If Dialog.OpenFile() Then Return
 
  sFileSource = Dialog.Path
  sFileTarget = sFileSource & sExtension
 
  Cp.Type = "bzlib2"
  Cp.File(sFileSource, sFileTarget, SpinBoxZLIB.Value)
 
End ' btnCompressBZLIB2_Click()

28.1.1.2 Comments

Download

Project

Download