我正在从文件夹中压缩文件(无法更改的 Ruby 1.8.6)。现在早些时候,文件中的行数减少了,压缩效果很好。但是当我向 zip 添加更多内容时,它给了我看起来像损坏内容的文件。这是长文件的排序视图。超过 5000 行的文件。
有什么方法可以压缩完整内容而不会损坏 .
内容喜欢
Family = 1,1,1
就是现在Fam
这是一个带有类的代码
def download
FileUtils.rm_rf Dir.glob("#{RAILS_ROOT}/#{params[:id]}/xx.zip")
zip_them_all = ZipThemAll.new("#{RAILS_ROOT}/#{params[:id]}/xxx.zip","#{RAILS_ROOT}/#{params[:id]}/")
zip_them_all.zip
end
class ZipThemAll
attr_accessor :list_of_file_paths, :zip_file_path
def initialize( zip_file_path, list_of_file_paths )
@zip_file_path = zip_file_path
list_of_file_paths = [list_of_file_paths] if list_of_file_paths.class == String
@list_of_file_paths = list_of_file_paths
end
def zip
zip_file = Zip::ZipFile.open(self.zip_file_path, Zip::ZipFile::CREATE)
self.zip_em_all( zip_file, @list_of_file_paths )
zip_file.close
end
def zip_em_all( zip_file, file_list, sub_directory=nil )
file_list.each do | file_path |
if File.exists?file_path
if File.directory?( file_path )
file_directory_list = []
# Find.find( file_path ) do | path |
file_directory_list = Dir.entries( file_path )
file_directory_list.delete(".")
file_directory_list.delete("..")
file_directory_list = file_directory_list.inject([]) do | result, path |
result << file_path + "/" + path
result
end
self.zip_em_all( zip_file, file_directory_list, (sub_directory == nil ? '.' : sub_directory) + "/" + File.basename(file_path) )
else
file_name = File.basename( file_path )
if sub_directory != nil
if zip_file.find_entry( sub_directory ) == nil
dir = zip_file.mkdir( sub_directory )
end
file_name = sub_directory + "/" + file_name
end
if zip_file.find_entry( file_name )
zip_file.replace( file_name, file_path )
else
zip_file.add( file_name, file_path)
end
end
else
puts "Warning: file #{file_path} does not exist"
end
end
end
end