Upload zip file with paperclip

Hi,

Senario: I have to upload a Zip file which contains flash scripts using
paperclip. Now I want unZip the file into a folder and save in rails
app.
I could able to upload a zip file. But failing to unzip to a folder in
filesystem

-------------------------- code sample from rails console-------------

?> p=Kproduct.find 3
=> #<Kproduct id: 3, title: “aaaa ddddd ddddddddddaaaaa”, kdescription:
“aaaaaaaa”, created_at: “2011-08-
03 12:47:17”, updated_at: “2011-08-03 12:47:25”, kcategory_id: 43, size:
9304, width: 269, height: 208, pa
rent_id: nil, content_type: “image/jpeg”, filename: “061.JPG”,
resolution: “Resizable”, opens_with: “Flash
8 (8+)”, price: 0, action_version: “AS3”, kmime: “application/zip”,
kpath: “/assets/3/FTsource.zip”, knam
e: “FTsource.zip”, ksize: 33, kadmime: nil, kadpath: nil, kadname: nil,
user_id: nil, thumbnail: nil, excl
usive: nil, user_tags: “aaa aaa aaaa”, category_id: 1,
author_comments: “”, photo_file_name: “0611111.JP
G”, photo_content_type: “image/jpeg”, photo_file_size: 11814,
photo_updated_at: “2011-08-03 12:46:49”, pre
view_file_name: “FTpreview.zip”, preview_content_type:
“application/zip”, preview_file_size: 6081523, prev
iew_updated_at: “2011-08-03 12:47:04”, source_file_name: “FTsource.zip”,
source_content_type: “application
/zip”, source_file_size: 6081523, source_updated_at: “2011-08-03
12:47:08”>

?> p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

?> file=p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

?> Zip::ZipFile.open(file) { |zip_file|
?> zip_file.each { |f|
?> f_path=File.join(destination, f.name)

    FileUtils.mkdir_p(File.dirname(f_path))
    zip_file.extract(f, f_path) unless File.exist?(f_path)
  }
}

Zip::ZipError: File /system/previews/3/original/FTpreview.zip?1312375624
not found
from
C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1396:in
initialize' from C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1410:innew’
from
C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1410:in
`open’
from (irb):23

<\Administrator\kmartapp\trunk\kmart\public\system\previews\3\original
Display all 568 possibilities? (y or n)
<Administrator\kmartapp\trunk\kmart\public\system\previews\3\original\FTpreview.zip’
=> “C:\Documents and
Settings\Administrator\kmartapp\trunk\kmart\public\system\previews\3\origin
al\FTpreview.zip”

<Administrator\kmartapp\trunk\kmart\public\system\previews\3\original\FTpreview.zip"
=> “C:Documents and SettingsAdministratorkmartapp\trunkkmartpublic
ystempreviews\003originalFTpreview.zip”

Zip::ZipFile.open(file) { |zip_file|
?> zip_file.each { |f|
?> f_path=File.join(destination, f.name)

    FileUtils.mkdir_p(File.dirname(f_path))
    zip_file.extract(f, f_path) unless File.exist?(f_path)
  }
}

Zip::ZipError: File C:Documents and SettingsAdministratorkmartapp
runkkmartpublic ystempreviews♥orig
inalFTpreview.zip not found
from
C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1396:in
initialize' from C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1410:innew’
from
C:/Ruby187/lib/ruby/gems/1.8/gems/rubyzip-0.9.4/lib/zip/zip.rb:1410:in
`open’
from (irb):34


Can anyone help me, why I cannot unZip file to folder.
Is it advisible to use paperclip for this type of upload.

On Aug 4, 7:32pm, Aashish K. [email protected] wrote:

?> p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

?> file=p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

url returns the url at which the file is available when accessed via
the web server - as you have demonstrated below, the file isn’t at /
system/previews/…
The path method returns the local path to the file.

Fred

Frederick C. wrote in post #1015275:

On Aug 4, 7:32pm, Aashish K. [email protected] wrote:

?> p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

?> file=p.preview.url
=> “/system/previews/3/original/FTpreview.zip?1312375624”

url returns the url at which the file is available when accessed via
the web server - as you have demonstrated below, the file isn’t at /
system/previews/…
The path method returns the local path to the file.

Fred

Even when I changed the path to like

file = 'C:\Documents and
settings\Administration\project\public\system\previews\3\original\FTpreview.zip

I get error as : …\FTpreview.zip not found.

Can you help me.

let me post new code.

On Aug 7, 10:46am, Aashish K. [email protected] wrote:

url returns the url at which the file is available when accessed via

I get error as : …\FTpreview.zip not found.

You’d need to escape the backslashes. Not sure how ruby handles
windows style paths.

Fred

Here is the code and error list below. After creating a zip file in
original folder. When I tried to run the from rails console, it works
great, but getting error when user tries to upload. So, I thought of
doing a delay job, and tried to run same code in show action but got
same error.

Any help would be great.

------------------code------------------------------
# unzip preview file after upload
require ‘rubygems’
require ‘zip’
file = RAILS_ROOT + ‘/system/previews/’ + self.id.to_s +
‘/original/’ + self.preview_file_name
destination = RAILS_ROOT + ‘/system/previews/’ + self.id.to_s +
‘/original/’
logger.info file
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
------------------error------------------------------------
Zip::ZipError (File C:/Documents and
Settings/Administrator/kmartapp/trunk/kmart/system/previews/10/origin
al/FTpreview.zip not found):
zip (2.0.2) lib/zip/zip.rb:1370:in initialize' zip (2.0.2) lib/zip/zip.rb:1384:innew’
zip (2.0.2) lib/zip/zip.rb:1384:in open' app/models/kproduct.rb:87:inunzip_file’
app/controllers/kproducts_controller.rb:16:in show' C:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/Ruby187/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in start' C:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/Ruby187/lib/ruby/1.8/webrick/server.rb:95:in start' C:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in start' C:/Ruby187/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/Ruby187/lib/ruby/1.8/webrick/server.rb:82:in `start’

Rendered rescues/_trace (62.5ms)
Rendered rescues/_request_and_response (15.6ms)
Rendering rescues/layout (internal_server_error)