0

Implementing the example code in ready.md, I get the error above. Searching through the source I can't find a method dest_file. The code I've implemented -

require 'rubygems'
require 'zip'

Zip::File.open('test.zip') do |zip_file|
  # Handle entries one by one
  zip_file.each do |entry|
    # Extract to file/directory/symlink
    puts "Extracting #{entry.name}"
    entry.extract(dest_file)

    # Read into memory
    content = entry.get_input_stream.read
  end
end

Have I understood this incorrectly? My assumption is that dest_file give the file the right metadata so it can be saved, but replacing with obvious entry.name throws an error.

dan
  • 1,030
  • 1
  • 9
  • 24

1 Answers1

1

You have not defined dest_file value. You need to specify the file name. May be you can use:

 entry.extract(entry.name)

to extract the file with same name as source file name and in the current directory.


If you want to extract to a specific dir, then, you could do something like below:

require "zip"

output_dir = "/tmp/"

Zip::File.open('a.zip') do |zip_file|
  # Handle entries one by one
  zip_file.each do |entry|
    # Extract to file/directory/symlink
    puts "Extracting #{entry.name}"
    entry.extract("#{File.expand_path(output_dir)}/#{entry.name}")
  end
end
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • Hi - I did look at that, but when you use entry.name with or without path you get `entry.rb:582:in `initialize': No such file or directory - OPS/chapter-1.xhtml (Errno::ENOENT)` - which is what made me think that dest_file is a method. – dan Jan 22 '16 at 12:24
  • I'm just looking at entry.rb - initialize to see fi I can work out what's going on..... – dan Jan 22 '16 at 12:26
  • Not sure what is the issue in your case, I have tested above suggestions before posting. They extract the files from a zip file. – Wand Maker Jan 22 '16 at 12:33
  • Hi - have just discovered the problem - I'm extracting an ePub, not a zip - had assumed they worked the same way, but they don't. Apologies. – dan Jan 22 '16 at 12:36
  • ....however, the issue you've answered is valid for a normal zip file - so thank you :) – dan Jan 22 '16 at 12:38