通过运行unix命令在ruby中创建zip文件,请不要使用gem
该脚本运行得非常好。我面临的唯一问题是路径。创建 zip 文件后。如果我解压它。它有一个完整的文件路径,例如:--
/name_the_file/Users/user_name/projects/project_name/public/system/files/10/original/*
我只想制作它
name_of_the_file/*
desc "Create Zip file"
task :create_zip => :environment do
directory_path = "#{RAILS_ROOT}/public/system/files/10/original"
bundle_filename="#{directory_path}/"+ "name_of_file.zip"
filenames = "#{directory_path}/*"
%x{ cd #{directory_path}}
%x{ zip -r #{bundle_filename} #{filenames}}
end
PS:- 我想创建 zip 文件。没有 tar、gzip 等
The script is working perfectly fine. The only issue I am facing is path. Once the zip file is created. If I unzip its. its having a complete path of the file like:--
/name_the_file/Users/user_name/projects/project_name/public/system/files/10/original/*
I just want to make it
name_of_the_file/*
desc "Create Zip file"
task :create_zip => :environment do
directory_path = "#{RAILS_ROOT}/public/system/files/10/original"
bundle_filename="#{directory_path}/"+ "name_of_file.zip"
filenames = "#{directory_path}/*"
%x{ cd #{directory_path}}
%x{ zip -r #{bundle_filename} #{filenames}}
end
PS:- I want to create zip files. No tar, gzip etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案如下:--
通常这会导致 zip 包含三个“子目录”:
使用 -j,您将得到:
Here is the solution:--
Normally this would result in a zip containing three "subdirs":
With -j, you get:
您将提供每个文件名的整个目录路径。由于您已经更改为该目录,因此无需执行此操作。
换句话说,如果您将
filenames
变量更改为:它应该按您的预期工作。
You are supplying the entire directory path with each filename. Since you have already changed to that directory, you don't need to do that.
In other words, if you change your
filenames
variable to:It should work as you intend.