开发者

losing data when zipping files

I am using rubyzip on windows to zip up a directory. When I unzip the archive some of the files are smaller than they were.

Zipping should be a lossless operation so I am wondering why this is hap开发者_Python百科pening.

Here is the code I am using:

require 'rubygems'
require 'find'
require 'zip/zip'

output = "c:/temp/test.zip"
zos = Zip::ZipOutputStream.new(output)

path = "C:/temp/profile"
::Find.find(path) do |file|
  next if File.directory?(file)
  entry = file.sub("#{path}/", '')

  zos.put_next_entry(entry)
  zos << File.read(file)
end
zos.close

The specific files that are having an issue are from a firefox profile. cert8.db and key3.db

Running the same code under jruby on linux with the same files works as expected - all the files are the same size.

Any ideas why this is a problem on windows?


I think problem is that you are reading files as text, not as binary files. These two fundamental modes of reading files have difference in such things as linebreaks, symbols EOF, etc.

Try File.open(file,'rb'){|f|f.read} instead of File.read(file).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜