File: zipfind.rb

package info (click to toggle)
ruby-zip 1.1.6-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 720 kB
  • ctags: 929
  • sloc: ruby: 7,045; makefile: 11
file content (74 lines) | stat: -rwxr-xr-x 1,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby

$VERBOSE = true

$: << "../lib"

require 'zip'
require 'find'

module Zip
  module ZipFind
    def self.find(path, zipFilePattern = /\.zip$/i)
      Find.find(path) {
	|fileName|
	yield(fileName)
	if zipFilePattern.match(fileName)  && File.file?(fileName)
	  begin
	    Zip::File.foreach(fileName)  {
	      |zipEntry|
	      yield(fileName + File::SEPARATOR + zipEntry.to_s)
	    }
	  rescue Errno::EACCES => ex
	    puts ex
	  end
	end
      }
    end

    def self.find_file(path, fileNamePattern, zipFilePattern = /\.zip$/i)
      self.find(path, zipFilePattern) {
	|fileName|
	yield(fileName) if fileNamePattern.match(fileName)
      }
    end

  end
end

if __FILE__ == $0
  module ZipFindConsoleRunner

    PATH_ARG_INDEX = 0;
    FILENAME_PATTERN_ARG_INDEX = 1;
    ZIPFILE_PATTERN_ARG_INDEX = 2;

    def self.run(args)
      check_args(args)
      Zip::ZipFind.find_file(args[PATH_ARG_INDEX],
			    args[FILENAME_PATTERN_ARG_INDEX],
			    args[ZIPFILE_PATTERN_ARG_INDEX]) {
	|fileName|
	report_entry_found fileName
      }
    end

    def self.check_args(args)
      if (args.size != 3)
	usage
	exit
      end
    end

    def self.usage
      puts "Usage: #{$0} PATH ZIPFILENAME_PATTERN FILNAME_PATTERN"
    end

    def self.report_entry_found(fileName)
      puts fileName
    end

  end

  ZipFindConsoleRunner.run(ARGV)
end