xlsx被rubyzip压缩,不能被Excel读取 [英] xlsx compressed by rubyzip not readable by Excel
问题描述
rubyzip
的gem来解压xlsx文件,然后立即将其压缩到新的归档文件,而不修改数据。然而,当我这样做,我无法打开新的excel文件,据说是被损坏。 或者,如果我使用Mac OS X的存档实用程序应用程序来处理zip文件),我解压缩并重新压缩Excel文件,数据没有损坏,我可以在Excel中打开结果文件。
我发现它不是coderubyzip 的解压缩功能,破坏数据,而拉链过程。 (实际上,当我使用 rubyzip
创建的新zip文件使用Archive Utility时,该文件可以再次由Excel读取)。
我想知道为什么会发生这种情况,以及可以通过Excel可读取的方式以内容编程方式来解决内容。
我的压缩代码:
def compress(path)
path.sub! (%r [/ $],'')
archive = File.join(path,File.basename(path))+'。zip'
FileUtils.rm archive,:force => true
Zip :: ZipFile.open(archive,'w')do | zipfile |
Dir [#{path} / ** / **]。reject {| f | f == archive} .each do | file |
temp = file
zipfile.add(file.sub(path +'/',''))文件)
end
end
end
OOXML格式对Zip的使用有一些限制以使包装符合要求。例如,包中允许的唯一压缩方法是DEFLATE。
您可能需要检查附件C中OPC包(哪个.XSLX文件)的规范标准可用这里(Zip),然后确保rubyzip库没有做任何不允许的事情(例如使用IMPLODE压缩方法)。
I am working on writing code which can read/write Excel xlsx files. xlsx files are simply zip archives of several xml files, so in order to test out if I could write a file, I used a gem called rubyzip
to unzip the xlsx file and then immediately zip it back up to a new archive, without modifying the data. When I do this, however, I cannot open the new excel file, it is said to be corrupted.
Alternatively, if I use Mac OS X's Archive Utility (the native application to handle zip files), and I unzip and re-zip an excel file, the data is not corrupted and I can open the resultant file in Excel.
I have found that it is not the 'unzip' functionality of rubyzip
that "corrupts" the data, but the zip process. (In fact, when I use Archive Utility on the new zip file that rubyzip
creates, the file is again readable by Excel).
I'm wondering why this happens, and what solutions there could be to zip the contents programmatically in a way which is readable by Excel.
My code for zipping:
def compress(path)
path.sub!(%r[/$],'')
archive = File.join(path,File.basename(path))+'.zip'
FileUtils.rm archive, :force=>true
Zip::ZipFile.open(archive, 'w') do |zipfile|
Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file|
temp = file
zipfile.add(file.sub(path+'/',''),file)
end
end
end
There are a number of constraints that the OOXML format imposes on the use of Zip in order for the packages to be conformant. For example, the only compression method permitted in the package is DEFLATE.
You might want to check the specification for OPC packages (which .XSLX files are) in Annex C of the standard available here (Zip), and then ensure that the rubyzip library is not doing anything that is not permitted (such as using the IMPLODE compression method).
这篇关于xlsx被rubyzip压缩,不能被Excel读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!