如何使用 rubyzip 重命名 zip 文件中的文件和目录
我正在尝试重命名 zip 中的文件和目录。我尝试了三种不同的方法,但都不起作用。执行此操作的正确命令是什么?
以下是我的代码摘录:
require 'zip/zip'
...
def renaming_zip(zip_file)
Zip::ZipFile.open(zip_file).each do |entry|
if entry.name == "mimetype"
puts "#{entry.name} is a file ? #{File.file? entry.name}"
puts " class ? #{entry.class}"
new_filename = "#{entry.name.gsub("mimetype", "#mimetype-new")}"
#found_entry = entry.get_entry("mimetype")
#found_entry.name = new_filename #1st try
puts " new filename #{new_filename}"
#File.rename(entry.name, new_filename) #2nd try
#entry.rename(entry.name, new_filename) #3rd try
end
end
end
如果我在没有任何重命名试验命令的情况下执行,我会得到此输出,因此您可以看到该文件存在于 zip 中。它只是不是一个 File 类,而是一个 Zip::ZipEntry 类,并且我能够解析新名称。
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
第一次尝试(未注释),我收到此错误:
mimetype is a file ? false
class ? Zip::ZipEntry
Uncaught exception: undefined method `get_entry' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:45:in `block in renaming_zip'
...
第二次尝试(未注释),我收到此错误:
Uncaught exception: No such file or directory - (mimetype, #mimetype-new)
/Users/.../app/lib/zip_rename.rb:48:in `rename'
/Users/.../app/lib/zxp_rename.rb:48:in `block in renaming_zip'
...
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
第三次尝试(未注释),我收到此错误:
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
Uncaught exception: undefined method `rename' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:49:in `block in renaming_zip'
...
I'm trying to rename a file and a directory in a zip. I'd tried three different, all not working. What is the right command to do it?
Below is the excerpt of my code:
require 'zip/zip'
...
def renaming_zip(zip_file)
Zip::ZipFile.open(zip_file).each do |entry|
if entry.name == "mimetype"
puts "#{entry.name} is a file ? #{File.file? entry.name}"
puts " class ? #{entry.class}"
new_filename = "#{entry.name.gsub("mimetype", "#mimetype-new")}"
#found_entry = entry.get_entry("mimetype")
#found_entry.name = new_filename #1st try
puts " new filename #{new_filename}"
#File.rename(entry.name, new_filename) #2nd try
#entry.rename(entry.name, new_filename) #3rd try
end
end
end
if I execute without any renaming trial command, I get this output, so you can see the file exists in the zip. It's just not a File class, but a Zip::ZipEntry class, and I'm able to parse the new name.
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
with 1st try (uncommented), I get this error:
mimetype is a file ? false
class ? Zip::ZipEntry
Uncaught exception: undefined method `get_entry' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:45:in `block in renaming_zip'
...
with 2nd try (uncommented), I get this error:
Uncaught exception: No such file or directory - (mimetype, #mimetype-new)
/Users/.../app/lib/zip_rename.rb:48:in `rename'
/Users/.../app/lib/zxp_rename.rb:48:in `block in renaming_zip'
...
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
with 3rd try( uncommented), I get this error:
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
Uncaught exception: undefined method `rename' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:49:in `block in renaming_zip'
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要重命名条目,请在条目上调用 rename。
第一次尝试失败,因为您正在
entry
上调用get_entry
,它应该位于ZipFile
上。第二次尝试失败,因为代码以双引号结束字符串。
第三次尝试失败,因为对象是
mimetype:Zip::ZipEntry
而不是Zip::ZipEntry
正确的方法是
To rename the entry call rename on the entry.
1st Attempt fails because you are calling
get_entry
onentry
, it should be onZipFile
.2nd Attempt fails because the code ends the string with double-quotes.
3rd Attempt fails because the object is
mimetype:Zip::ZipEntry
and is notZip::ZipEntry
The correct way to do it is
要使用 rubyzip 重命名 zip 中的文件:
To rename file inside zip with rubyzip: