Class: Archive::Compress

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/compress.rb

Overview

Compression OOP interface for Archive. See ::new and #compress for more information.

Constant Summary collapse

BUFSIZE =

The buffer size for reading content.

32767

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, args = { :type => :tar, :compression => :gzip }) ⇒ Compress

Create a new Compress object. Takes a filename as string, and args as hash.

args is a hash that contains two values, :type and :compression.

  • :type may be :tar or :zip

  • :compression may be :gzip, :bzip2, or nil (no compression)

If the type :zip is selected, no compression will be used. Additionally, files in the .zip will all be stored as binary files.

The default set of arguments is

{ :type => :tar, :compression => :gzip }


34
35
36
37
38
39
40
41
42
# File 'lib/archive/compress.rb', line 34

def initialize(filename, args={ :type => :tar, :compression => :gzip })
  @filename     = filename
  @type         = args[:type] || :tar
  @compression  = args[:compression]

  if type == :zip
    @compression = nil
  end
end

Instance Attribute Details

#compressionObject (readonly)

the compression type of the archive. See ::new.



13
14
15
# File 'lib/archive/compress.rb', line 13

def compression
  @compression
end

#filenameObject (readonly)

the filename of the compressed archive



9
10
11
# File 'lib/archive/compress.rb', line 9

def filename
  @filename
end

#typeObject (readonly)

the type of the archive. See ::new.



11
12
13
# File 'lib/archive/compress.rb', line 11

def type
  @type
end

Instance Method Details

#compress(files, verbose = false) ⇒ Object

Run the compression. Files are an array of filenames. Optional flag for verbosity; if true, will print each file it adds to the archive to stdout.

Files must be real files. No symlinks, directories, unix sockets, character devices, etc. This method will raise ArgumentError if you provide any.



53
54
55
56
57
58
59
60
61
# File 'lib/archive/compress.rb', line 53

def compress(files, verbose=false)
  if files.any? { |f| !File.file?(f) }
    raise ArgumentError, "Files supplied must all be real, actual files -- not directories or symlinks."
  end

  configure_archive
  compress_files(files, verbose)
  free_archive
end