Как мне адаптировать образец RubyZip для использования File.write_buffer вместо File.open при создании zip-файла? - PullRequest
0 голосов
/ 01 июля 2018

Как мне адаптировать следующий пример, который использует File.open при создании zip-файла, с использованием write_buffer вместо File.open, как указано в следующем примере кода?

Вот пример File.open:

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 

Вот пример write_buffer, к которому я хотел бы адаптировать приведенный выше код:

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) }

Заранее спасибо за помощь. Вот моя попытка адаптации, но я чувствую себя потерянным.

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
...