unzip (zip, tar, tag.gz) files with ruby

前端 未结 4 1905
长发绾君心
长发绾君心 2020-12-01 00:30

I want to unzip a lot of zip files. Is there a module or script that checks which format the zip file is and decompresses it? This should work on Linux, I don\'t care about

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 00:42

    The easiest way is to probably use Zlib

    Zlib is a Ruby library. What follows is a simple program to grab a Zipped file from a particular URL, unzip it, and paste its contents to the screen.

    require 'zlib' 
    require 'open-uri'
    
    uri = "www.somedomain.com/filename.gz"
    source = open(uri)
    gz = Zlib::GzipReader.new(source) 
    result = gz.read
    puts result
    

    I hope this helps.

提交回复
热议问题