Class: Presso

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

Defined Under Namespace

Modules: JavaUtilZip

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#unzip(input_path, output_directory) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/presso.rb', line 39

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)
    stream_io = stream.to_io
    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))
          File.open(entry.name, 'w') do |output|
            IO.copy_stream(stream_io, output)
          end
        end
        stream.close_entry
      end
    end
    stream_io.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
33
34
35
36
37
# 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)
      stream_io = stream.to_io
      Dir['**/*'].each do |path|
        if File.file?(path)
          stream.put_next_entry(JavaUtilZip::ZipEntry.new(path))
          File.open(path) do |input|
            IO.copy_stream(input, stream_io)
          end
          stream_io.flush
        elsif File.directory?(path)
          stream.put_next_entry(JavaUtilZip::ZipEntry.new(path+'/'))
        else
          raise PressoError, "File #{path} is not a regular file."
        end
        stream.close_entry
      end
      stream_io.close
    end
  end
end