2013-07-30 5 views
4

내 응용 프로그램에서 rubyzip을 사용하여 zip 아카이브를 생성하려고합니다. 나는 their readme을 출발점으로 사용하고 있습니다. 내 Gemfile에 보석을 추가 : rubyzip - 해당 파일을로드 할 수 없습니다. - zip

gem 'rubyzip' 

그런 다음 bundle install를 실행하고 레일 서버를 다시 시작됩니다.

내 컨트롤러 코드는 다음과 같습니다 cannot load such file -- zip

내가 require 'zip/zip' 시도했지만이 같은 오류가 발생합니다 :

require 'rubygems' 
require 'zip' 

filename = "#{Time.now.strftime('%y%m%d%H%M%S')}" 
input_filenames = ["#{filename}.txt"] 
zip_filename = "#{filename}.zip" 

Zip::File.open(zip_filename, Zip::File::CREATE) do |zipfile| 
    input_filenames.each do |f| 
    zipfile.add(f, directory + '/' + f) 
    end 
end 

내가 갖는 오류입니다.

미리 감사드립니다.

+0

'필요'rubygems'', bundler 당신을 위해 그것을 돌봐. – levinalex

+0

불행히도, 아니오. 'require 'zip'을 주석 처리하면 '초기화되지 않은 상수 ... :: Zip' 오류가 발생합니다. – seancdavis

+0

여전히'require "zip"'이 필요합니다 (rubyzip의 어떤 버전을 btw로 사용합니까?) – levinalex

답변

11

너무 새로운 예가 될 수 있습니다.

당신이 Rubyzip 0.9.x를 사용하는 경우, 당신은 this example을 따라야합니다

rubyzip의

require 'zip/zip' 

folder = "Users/me/Desktop/stuff_to_zip" 
input_filenames = ['image.jpg', 'description.txt', 'stats.csv'] 

zipfile_name = "/Users/me/Desktop/archive.zip" 

Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile| 
    input_filenames.each do |filename| 
    # Two arguments: 
    # - The name of the file as it will appear in the archive 
    # - The original file, including the path to find it 
    zipfile.add(filename, folder + '/' + filename) 
    end 
end 
+0

굉장! 이것은 완벽하게 작동했습니다. 나는 0.9.9를 사용하고있다. 도와 줘서 고마워. – seancdavis

+0

보석 문서가 구식 인 것처럼 보입니다.이 파일들은'Zip :: File.open (zipfile_name, Zip :: File :: CREATE) do | zipfile |'을 가지고 있지만, 소스 코드를보고 나서,'Zip : : ZipFile ... '이 맞습니다. 나는 그들을 알릴 것이다. –

2

인터페이스가 변경되었습니다 (require "zip/zip"은 다음 Zip::ZipFile 대신 Zip::File의 사용). 이전 버전을 사용 중입니다. 마스터 브랜치에서 rubydoc.info에 관한 문서.

5

내가 사용하는 것이 좋습니다 : 컨트롤러에

gem 'rubyzip', "~> 1.1", require: 'zip' 

다음 require 'zip'가 잘 작동합니다.

+0

서버를 다시 시작해야합니다. – venkatvb

관련 문제