write_buffer 而不是打开的 RubyZip docx 问题
RubyZip docx issues with write_buffer instead of open
我正在调整 RubyZip 递归压缩示例 (found here) to work with write_buffer instead of open and am coming across a host of issues. I'm doing this because the zip archive I'm producing has word documents in it and I'm getting errors on opening those word documents. Therefore, I'm trying the work-around that RubyZip suggests, which is using write_buffer instead of open (example found here)。
问题是,我遇到错误是因为我使用的是绝对路径,但我不确定如何解决这个问题。我收到错误消息“#//',名称不能以 /> 开头”
其次,我不确定如何缓解 word 文档的问题。当我使用我的原始代码时,该代码有效并创建了一个实际的 zip 文件,该 zip 文件中的任何 word 文档在打开时都会出现以下错误:"Word found unreadable content in Do you want to recover the contents of this document? If you trust the source of this document, click Yes." The unreadable content error 是我尝试尝试的原因使用 write_buffer.
如有任何帮助,我们将不胜感激。
这是我目前使用的代码:
require 'zip'
require 'zip/zipfilesystem'
module AdvisoryBoard
class ZipService
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
path = ""
buffer = Zip::ZipOutputStream.write_buffer do |zipfile|
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
@file = nil
@data = nil
if !File.directory?(disk_file_path)
@file = File.open(disk_file_path, "r+b")
@data = @file.read
unless [@output_file, @input_dir].include?(e)
zipfile.put_next_entry(e)
zipfile.write @data
end
@file.close
end
end
zipfile.put_next_entry(@output_file)
zipfile.put_next_entry(@input_dir)
end
File.open(@output_file, "wb") { |f| f.write(buffer.string) }
end
end
end
我能够在没有任何警告或损坏的情况下打开 word 文档!这是我最后做的:
require 'nokogiri'
require 'zip'
require 'zip/zipfilesystem'
class ZipService
# Initialize with the directory to zip and the location of the output archive.
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
write_entries entries, '', zipfile
end
end
private
# A helper method to make the recursion work.
def write_entries(entries, path, zipfile)
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
else
put_into_archive(disk_file_path, zipfile, zipfile_path, e)
end
end
end
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
zipfile.mkdir zipfile_path
subdir = Dir.entries(disk_file_path) - %w[. ..]
write_entries subdir, zipfile_path, zipfile
end
def put_into_archive(disk_file_path, zipfile, zipfile_path, entry)
if File.extname(zipfile_path) == ".docx"
Zip::File.open(disk_file_path) do |zip|
doc = zip.read("word/document.xml")
xml = Nokogiri::XML.parse(doc)
zip.get_output_stream("word/document.xml") {|f| f.write(xml.to_s)}
end
zipfile.add(zipfile_path, disk_file_path)
else
zipfile.add(zipfile_path, disk_file_path)
end
end
end
我正在调整 RubyZip 递归压缩示例 (found here) to work with write_buffer instead of open and am coming across a host of issues. I'm doing this because the zip archive I'm producing has word documents in it and I'm getting errors on opening those word documents. Therefore, I'm trying the work-around that RubyZip suggests, which is using write_buffer instead of open (example found here)。
问题是,我遇到错误是因为我使用的是绝对路径,但我不确定如何解决这个问题。我收到错误消息“#//',名称不能以 /> 开头”
其次,我不确定如何缓解 word 文档的问题。当我使用我的原始代码时,该代码有效并创建了一个实际的 zip 文件,该 zip 文件中的任何 word 文档在打开时都会出现以下错误:"Word found unreadable content in Do you want to recover the contents of this document? If you trust the source of this document, click Yes." The unreadable content error 是我尝试尝试的原因使用 write_buffer.
如有任何帮助,我们将不胜感激。
这是我目前使用的代码:
require 'zip'
require 'zip/zipfilesystem'
module AdvisoryBoard
class ZipService
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
path = ""
buffer = Zip::ZipOutputStream.write_buffer do |zipfile|
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
@file = nil
@data = nil
if !File.directory?(disk_file_path)
@file = File.open(disk_file_path, "r+b")
@data = @file.read
unless [@output_file, @input_dir].include?(e)
zipfile.put_next_entry(e)
zipfile.write @data
end
@file.close
end
end
zipfile.put_next_entry(@output_file)
zipfile.put_next_entry(@input_dir)
end
File.open(@output_file, "wb") { |f| f.write(buffer.string) }
end
end
end
我能够在没有任何警告或损坏的情况下打开 word 文档!这是我最后做的:
require 'nokogiri'
require 'zip'
require 'zip/zipfilesystem'
class ZipService
# Initialize with the directory to zip and the location of the output archive.
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
write_entries entries, '', zipfile
end
end
private
# A helper method to make the recursion work.
def write_entries(entries, path, zipfile)
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
else
put_into_archive(disk_file_path, zipfile, zipfile_path, e)
end
end
end
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
zipfile.mkdir zipfile_path
subdir = Dir.entries(disk_file_path) - %w[. ..]
write_entries subdir, zipfile_path, zipfile
end
def put_into_archive(disk_file_path, zipfile, zipfile_path, entry)
if File.extname(zipfile_path) == ".docx"
Zip::File.open(disk_file_path) do |zip|
doc = zip.read("word/document.xml")
xml = Nokogiri::XML.parse(doc)
zip.get_output_stream("word/document.xml") {|f| f.write(xml.to_s)}
end
zipfile.add(zipfile_path, disk_file_path)
else
zipfile.add(zipfile_path, disk_file_path)
end
end
end