我有一个非常大的tarball,里面有大约500个文件。
我正在编写一个程序,使用package
和zlib
从此tarball中提取单个文件。我的问题是,如何从此tarball中提取单个文件, tar里面有一个名为home/common/4000_ss
的目录,我可以在不带文件的目录的情况下提取这个文件吗?
来源:
#!/usr/local/bin/ruby
require 'rubygems/package'
require 'fileutils'
require 'zlib'
class ArchiveManager
attr_accessor :folio_num
def initialize(folio_num)
@folio_num = folio_num
end
def num_check
if folio_num =~ /^\d{4,7}$/ #<= Thank you Tin Man.
search_for_spreadsheet
else
puts <<-EOF
USAGE: unarchive_ss <folio number>
EOF
end
end
def search_for_spreadsheet
location = FileUtils.chdir("/home/<dir>/<user>/archive")
digits = `grep -lr #{folio_num}.ods *.txt`
file_name = digits.match(/\d+/).to_s
puts "Spreadsheet found in UW_archive_#{file_name}.tgz"
extract(file_name, location)
end
def extract(file_name, location)
extract_tar = Gem::Package::TarReader.new(Zlib::GzipReader.open("/home/<dir>/<user>/archive/UW_archive_#{file_name}.tgz"))
extract_tar.rewind
extract_tar.each do |tar|
puts tar.full_name
puts tar.directory?
puts tar.file?
end
end
end
#/2715015
test = ArchiveManager.new(ARGV[0])
test.num_check
现在的使用示例:
[]$ ruby unarchive_ss 2715015 #I know I'm asking it to do all of this. home/common/4000_UW_spreadsheets/2719102.ods false true home/common/4000_UW_spreadsheets/2718427.ods false true home/common/4000_UW_spreadsheets/2721372.ods false true home/common/4000_UW_spreadsheets/2718986.ods false true home/common/4000_UW_spreadsheets/2718893.ods false true home/common/4000_UW_spreadsheets/2719048.ods false true home/common/4000_UW_spreadsheets/2719572.ods false true home/common/4000_UW_spreadsheets/10853.ods false true home/common/4000_UW_spreadsheets/2719161.ods false true home/common/4000_UW_spreadsheets/2719764.ods false true home/common/4000_UW_spreadsheets/2718473.ods false true home/common/4000_UW_spreadsheets/2718712.ods false true