User Tools

Site Tools


k28:k28.1:k28.1.1:start

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

  • First, the ranges for the compression level are set (SpinBoxes) and the default compression level for the two compression drivers is set.
  • Then a Compress object is created and the file extension for the compression driver used is set.
  • Then you can select the file to be compressed in the dialogue. The file filter in the project is set to csv files. You can adapt this setting to your preferences. In this case, the generated archive file with the assigned extension has the same name as the original file and is saved in the same directory as the original file.
  • Finally, the selected file is packed according to the selected algorithm (Type) and with the set compression level (SpinBox) using the paths for the source and target files.

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k28/k28.1/k28.1.1/start.txt · Last modified: 31.01.2022 (external edit)

Page Tools