用ruby解压缩(zip,tar,tag.gz)文件 [英] unzip (zip, tar, tag.gz) files with ruby

查看:461
本文介绍了用ruby解压缩(zip,tar,tag.gz)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压缩很多zip文件.是否有模块或脚本检查zip文件的格式并对其进行解压缩? 这应该可以在Linux上运行,我不在乎其他操作系统.

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 other OSs.

推荐答案

要从.tar.gz文件中提取文件,可以从与Ruby一起分发的包中使用以下方法:

To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby:

require 'rubygems/package'
require 'zlib'
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open('Path/To/myfile.tar.gz'))
tar_extract.rewind # The extract has to be rewinded after every iteration
tar_extract.each do |entry|
  puts entry.full_name
  puts entry.directory?
  puts entry.file?
  # puts entry.read
end
tar_extract.close

每个类型为 Gem ::的条目: Package :: TarReader :: Entry 指向.tar.gz文件中的文件或目录.

Each entry of type Gem::Package::TarReader::Entry points to a file or directory within the .tar.gz file.

可以使用类似的代码(将Writer替换为Reader)将文件写入.tar.gz文件.

Similar code can be used (replace Reader with Writer) to write files to a .tar.gz file.

这篇关于用ruby解压缩(zip,tar,tag.gz)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆