Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:57:10+00:00 2026-06-18T09:57:10+00:00

I want to compress some files in Ruby on Rails and save the zip

  • 0

I want to compress some files in Ruby on Rails and save the zip file in the tmp folder. I’ve got a Document model which has a name field with an associated uploader. I’m also using Carrierwave to upload files to Amazon S3. I’ve got the following code:

class Document < ActiveRecord::Base

  mount_uploader :name, DocumentUploader
  ...

end


def create_zip  
  documents = Document.all
  folder = "#{Rails.root}/tmp"
  tmp_filename = "#{folder}/export.zip"

  zip_path = tmp_filename
  Zip::ZipFile::open(zip_path, true) do |zipfile|
    documents.each do |photo|
      zipfile.get_output_stream(document.name.identifier) do |io|
        io.write document.name.file.read
      end
    end
  end

end

This creates an export.zip file in my tmp folder, but when I try to open it, Archive Manager (Mac OS X) begins unarchiving it, but keeps doing it so without ever finishing. I believe there’s something missing from my code. The zip file size does make sense to me, but I’ve got that problem. Any thoughts? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:57:12+00:00Added an answer on June 18, 2026 at 9:57 am

    Actually, I found out I could open the zip file using other program (zipeg). However, only the last file from the documents array was in the compressed file. I believe I had been overwriting previous files, as the only remaining file was called the same (export, as the name of the zip itself) in all cases.

    The code bellow works for me:

    def create_zip
      documents = Document.all
    
      folder = "#{Rails.root}/tmp"
      tmp_filename = "#{folder}/export.zip"
      zip_path = tmp_filename
    
      Zip::ZipOutputStream.open(zip_path) do |zos|
        documents.each do |document|
          path = document.name_identifier
          zos.put_next_entry(path)
          zos.write photo.name.file.read
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compress some files (into the ZIP format) and encrypt them if
I get the error Not implemented. I want to compress a file using 7-Zip
I need to pre-compress some very large html/xml/json files (large data dumps) using either
I got a java program which uses a JavaCompiler to generate new class files.
I'm developing an Android 2.2 application. I want to add some big text files
So there are some folders that I would like to compress into tar.gz file
I have some grayscale jpeg files and I want to convert them to bmp
I want to add in my application functionality to decompress (and optionally compress) files
I have some commands that I'm using to compress a file on saving, and
I've written some code to bz2-compress a file with the BZ2 Compressor Object: def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.