0

How do I adapt the following sample that utilizes File.open when constructing a zip file to utilizing write_buffer instead of File.open as given in the next code sample?

Here is the File.open sample:

require 'zip'

# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
#   directoryToZip = "/tmp/input"
#   outputFile = "/tmp/out.zip"
#   zf = ZipFileGenerator.new(directoryToZip, outputFile)
#   zf.write()
class ZipFileGenerator

  # Initialize with the directory to zip and the location of the output archive.
  def initialize(inputDir, outputFile)
    @inputDir = inputDir
    @outputFile = outputFile
  end

  # Zip the input directory.
  def write()
    entries = Dir.entries(@inputDir); 
    entries.delete("."); 
    entries.delete("..")
    io = Zip::File.open(@outputFile, Zip::File::CREATE);

    writeEntries(entries, "", io)
    io.close();
  end



  # A helper method to make the recursion work.
  private
  def writeEntries(entries, path, io)

    entries.each { |e|
      zipFilePath = path == "" ? e : File.join(path, e)
      diskFilePath = File.join(@inputDir, zipFilePath)
      puts "Deflating " + diskFilePath
      if  File.directory?(diskFilePath)
        io.mkdir(zipFilePath)
        subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
        writeEntries(subdir, zipFilePath, io)
      else
        io.get_output_stream(zipFilePath) { |f| 
f.puts(File.open(diskFilePath, "rb").read())}
      end
    }
  end
end 

Here is the write_buffer sample that I would like the above code to be adapted to:

buffer = Zip::OutputStream.write_buffer do |out|
  @zip_file.entries.each do |e|
    unless [DOCUMENT_FILE_PATH, RELS_FILE_PATH].include?(e.name)
      out.put_next_entry(e.name)
      out.write e.get_input_stream.read
     end
  end

  out.put_next_entry(DOCUMENT_FILE_PATH)
  out.write xml_doc.to_xml(:indent => 0).gsub("\n","")

  out.put_next_entry(RELS_FILE_PATH)
  out.write rels.to_xml(:indent => 0).gsub("\n","")
end

File.open(new_path, "wb") {|f| f.write(buffer.string) }

Thanks in advance for your help. Here is my adaptation attempt, but I feel lost.

def writeViaBuffer()
    entries = Dir["#{@inputDir}/**/*"]; entries.delete("."); entries.delete("..")
    buffer = Zip::OutputStream.write_buffer do |out|
     entries.each do |e|
       byebug
       @file = nil
       @data = nil
       if !File.directory?(e)
         @file = File.open(e, "r+b")
         @data = @file.read


         unless [@outputFile, @inputDir].include?(e)
           out.put_next_entry(e)
           out.write @data
         end
         @file.close
       end
   end

   out.put_next_entry(@outputFile)
   out.write xml_doc.to_xml(:indent => 0).gsub("\n","")

   out.put_next_entry(@inputDir)
   out.write rels.to_xml(:indent => 0).gsub("\n","")
   end

   File.open(new_path, "wb") {|f| f.write(buffer.string) }  
 end
Davie Overman
  • 114
  • 1
  • 13
  • What do you mean, "how do I adapt"? Can you be more specific? Otherwise it sounds like you asking us to do all the work here (including deciphering what is it that both snippets do) – Sergio Tulentsev Jul 01 '18 at 16:52
  • Sergio, I think I need to ask is my attempt that I included on the right track? When I run the function, I get "Illegal ZipEntry name '/tmp/d20180701-14522-c1f3zz/word/document.xml', name must not start with / " and I've no clue if I'm handling directories correctly. – Davie Overman Jul 01 '18 at 17:19
  • Yes, your handling of directories is a bit off. You open all files in `@inputDir` and zip them as is, but zip file entry path must be a relative path (not including `@inputDir`). – Sergio Tulentsev Jul 01 '18 at 17:25

0 Answers0