rubyzip/samples/zipfind.rb

68 lines
1.4 KiB
Ruby
Raw Normal View History

2002-09-15 04:59:29 +08:00
#!/usr/bin/env ruby
$VERBOSE = true
2015-03-21 16:27:44 +08:00
$: << '../lib'
2002-09-15 04:59:29 +08:00
2014-03-31 19:20:27 +08:00
require 'zip'
2002-09-15 04:59:29 +08:00
require 'find'
2002-09-15 23:16:39 +08:00
module Zip
module ZipFind
def self.find(path, zipFilePattern = /\.zip$/i)
2015-03-21 16:10:37 +08:00
Find.find(path) do |fileName|
yield(fileName)
2015-03-24 00:27:20 +08:00
next unless zipFilePattern.match(fileName) && File.file?(fileName)
2015-03-24 00:27:20 +08:00
begin
Zip::File.foreach(fileName) do |zipEntry|
yield(fileName + File::SEPARATOR + zipEntry.to_s)
2015-03-21 16:10:37 +08:00
end
rescue Errno::EACCES => e
puts e
2015-03-21 16:10:37 +08:00
end
end
2002-09-15 23:16:39 +08:00
end
def self.find_file(path, fileNamePattern, zipFilePattern = /\.zip$/i)
2015-03-23 00:30:24 +08:00
find(path, zipFilePattern) do |fileName|
2015-03-21 16:10:37 +08:00
yield(fileName) if fileNamePattern.match(fileName)
end
2002-09-15 23:16:39 +08:00
end
2002-09-15 04:59:29 +08:00
end
end
2017-06-29 10:57:12 +08:00
if $0 == __FILE__
2002-09-15 23:16:39 +08:00
module ZipFindConsoleRunner
2015-03-23 01:10:52 +08:00
PATH_ARG_INDEX = 0
FILENAME_PATTERN_ARG_INDEX = 1
ZIPFILE_PATTERN_ARG_INDEX = 2
2014-03-31 19:20:27 +08:00
2002-09-15 23:16:39 +08:00
def self.run(args)
check_args(args)
2014-03-31 19:20:27 +08:00
Zip::ZipFind.find_file(args[PATH_ARG_INDEX],
2015-03-21 15:44:56 +08:00
args[FILENAME_PATTERN_ARG_INDEX],
2015-03-21 16:10:37 +08:00
args[ZIPFILE_PATTERN_ARG_INDEX]) do |fileName|
report_entry_found fileName
end
2002-09-15 23:16:39 +08:00
end
2014-03-31 19:20:27 +08:00
def self.check_args(args)
2020-02-09 23:50:00 +08:00
return if args.size == 3
usage
exit
2002-09-15 23:16:39 +08:00
end
def self.usage
puts "Usage: #{$0} PATH ZIPFILENAME_PATTERN FILNAME_PATTERN"
end
2014-03-31 19:20:27 +08:00
def self.report_entry_found(fileName)
2002-09-15 23:16:39 +08:00
puts fileName
end
2002-09-15 04:59:29 +08:00
end
2002-09-15 23:16:39 +08:00
ZipFindConsoleRunner.run(ARGV)
2002-09-15 04:59:29 +08:00
end