Mohit Jain
Mohit Jain

Reputation: 43939

Create zip files in ruby by running unix commands, no gems please

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

Upvotes: 0

Views: 945

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 43939

Here is the solution:--

  %x{  zip -r -j  #{bundle_filename}  #{filenames}}

Normally this would result in a zip containing three "subdirs":

a/
+ file1

b/
+ file2

c/
+ file3

With -j, you get:

./
+ file1
+ file2
+ file3

Upvotes: 2

Mark Thomas
Mark Thomas

Reputation: 37517

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:

filenames = "*"

It should work as you intend.

Upvotes: 1

Related Questions