Ruby ZipFile.extract未保留权限
我已经编写了一个Ruby方法来解压缩zip文件(根据要求,我不打算使用shellRuby ZipFile.extract未保留权限,ruby,Ruby,我已经编写了一个Ruby方法来解压缩zip文件(根据要求,我不打算使用shellunzip命令)。但它并没有保留权限。有很多文件(zipfile相当大),所以我不想手动更新权限。有人能帮我解决这个问题吗 下面是解压构建的一段代码 def unzip_file (file_path, destination_path) begin Zip::ZipFile.open(file_path) { |zip_file| zip_file.each
unzip
命令)。但它并没有保留权限。有很多文件(zipfile相当大),所以我不想手动更新权限。有人能帮我解决这个问题吗
下面是解压构建的一段代码
def unzip_file (file_path, destination_path)
begin
Zip::ZipFile.open(file_path) { |zip_file| zip_file.each { |f|
f_path=File.join(destination_path, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
rescue => e
$LOG.error "Error occurred in Utils.unzip_file - #{e}"
end
end
默认情况下,
zip
gem不会恢复权限。在提取其条目之前,必须将ZipFile
对象上的restore\u permissions
属性设置为true。因此,请使用以下命令:
Zip::ZipFile.open(file_path) do |zip_file|
zip_file.restore_permissions = true
zip_file.each do |f|
# ...
end
end
如果您也想恢复所有权,还可以设置一个
restore\u ownership
属性。下面是一段代码,它将解压缩zipfile并保留权限
def unzip_file (file_path, destination_path)
begin
Zip::ZipFile.open(file_path) { |zip_file|
zip_file.restore_permissions = true
zip_file.each { |f|
f_path=File.join(destination_path, f.name)
FleUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
rescue => e
$LOG.error "Error occurred in Utils.unzip_file - #{e}"
end
end
Ruby中的标准缩进是2个空格,不是3,不是8,不是11,当然也不是32。