Class: Presso

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

Defined Under Namespace

Modules: JavaUtilZip

Constant Summary collapse

VERSION =
'1.0.0'.freeze
PressoError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#unzip(input_path, output_directory) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/presso.rb', line 34

def unzip(input_path, output_directory)
  raise PressoError, "Target directory #{output_directory} already exists." if File.exists?(output_directory)
  File.open(input_path, 'rb') do |file|
    stream = JavaUtilZip::ZipInputStream.new(file.to_inputstream)
    FileUtils.mkdir_p(output_directory)
    Dir.chdir(output_directory) do
      while (entry = stream.next_entry)
        if entry.directory?
          FileUtils.mkdir_p(entry.name)
        else
          FileUtils.mkdir_p(File.dirname(entry.name))
          IO.copy_stream(stream.to_io, entry.name)
        end
        stream.close_entry
      end
    end
    stream.close
  end
end

#zip_dir(output_path, input_directory) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/presso.rb', line 14

def zip_dir(output_path, input_directory)
  output_path = File.expand_path(output_path)
  Dir.chdir(input_directory) do
    File.open(output_path, File::WRONLY|File::CREAT|File::EXCL, binmode: true) do |file|
      stream = JavaUtilZip::ZipOutputStream.new(file.to_outputstream)
      Dir['**/*'].each do |path|
        if File.file?(path)
          stream.putNextEntry(JavaUtilZip::ZipEntry.new(path))
          IO.copy_stream(path, stream.to_io)
        elsif File.directory?(path)
          stream.putNextEntry(JavaUtilZip::ZipEntry.new(path+'/'))
        else
          raise PressoError, "File #{path} is not a regular file."
        end
      end
      stream.close
    end
  end
end