zip ruby ã«ã¯ buffer ã¢ã¼ããæãã
zipruby ããã®ã¾ã¾ä½¿ãã¨ãä¸æãã¡ã¤ã«ãä½ã£ã¦åºåå ã«zip ãç¨æããªãã¡ããããªãã®ã§ãtmpfile ãªã©ã¨çµã¿åãããå¿ è¦ãããã©ãå¤æ°ã ãã§ã¡ã¢ãªå é¨ã§ã±ã±ã£ã¨ä½ããªãã®ã調ã¹ãã
ruby-zip-in-memory-sample.rb
#!/usr/bin/env ruby # require 'zipruby' zip_bin = '' # ããã«zipã®ãã¤ããªãå ¥ã£ã¦ãã Zip::Archive.open_buffer(zip_bin){|ar| ar.add_buffer('sample.txt', "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n") } ## 確èªç¨ã«ãã¡ã¤ã«ã«æ¸ãåºãã open('out.zip','w'){|f| f.write zip_bin }
å®éã«ä½¿ã£ã¦ã¿ã
å®è¡ããã¨ç¡äºã« zip ãã¡ã¤ã«ãçæãããã
takuya@~/Desktop/$ ruby ruby-zip-in-memory-sample.rb takuya@~/Desktop/$ ll -rw-r--r-- 1 takuya staff 123 2016-09-26 01:20 out.zip -rw-r--r-- 1 takuya staff 206 2016-09-26 01:20 ruby-zip-in-memory-sample.rb
ã¡ããã¨zip ãåºæ¥ã¦ããã確èªããã
takuya@~/Desktop/$ lsar out.zip out.zip: Zip sample.txt
å±éãã¦ä¸èº«ã確èª
takuya@~/Desktop/$ unar out.zip out.zip: Zip sample.txt (30 B)... OK. Successfully extracted to "./sample.txt". takuya@~/Desktop/$ cat sample.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ãã¡ã¤ã«ã使ããã«zip ã¢ã¼ã«ã¤ããä½ãããã¨ãããã£ãã
zip ã¢ã¼ã«ã¤ããä½ã£ã¦responseããã®ã«æ¯åãã¡ã¤ã«ã«æ¸ãã¦ããã®ããã«ããããã¦ãããã¨ãã¡ã¤ã«ãæ®ã£ã¦ãã¾ãã®ãå«ã ãã