mezza-rubyzip 0.9.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
1
+ # With ziprequire you can load ruby modules from a zip file. This means
2
+ # ruby's module include path can include zip-files.
3
+ #
4
+ # The following example creates a zip file with a single entry
5
+ # <code>log/simplelog.rb</code> that contains a single function
6
+ # <code>simpleLog</code>:
7
+ #
8
+ # require 'zip/zipfilesystem'
9
+ #
10
+ # Zip::ZipFile.open("my.zip", true) {
11
+ # |zf|
12
+ # zf.file.open("log/simplelog.rb", "w") {
13
+ # |f|
14
+ # f.puts "def simpleLog(v)"
15
+ # f.puts ' Kernel.puts "INFO: #{v}"'
16
+ # f.puts "end"
17
+ # }
18
+ # }
19
+ #
20
+ # To use the ruby module stored in the zip archive simply require
21
+ # <code>zip/ziprequire</code> and include the <code>my.zip</code> zip
22
+ # file in the module search path. The following command shows one
23
+ # way to do this:
24
+ #
25
+ # ruby -rzip/ziprequire -Imy.zip -e " require 'log/simplelog'; simpleLog 'Hello world' "
26
+
27
+ #$: << 'data/rubycode.zip' << 'data/rubycode2.zip'
28
+
29
+
30
+ require 'zip/zip'
31
+
32
+ class ZipList #:nodoc:all
33
+ def initialize(zipFileList)
34
+ @zipFileList = zipFileList
35
+ end
36
+
37
+ def get_input_stream(entry, &aProc)
38
+ @zipFileList.each {
39
+ |zfName|
40
+ Zip::ZipFile.open(zfName) {
41
+ |zf|
42
+ begin
43
+ return zf.get_input_stream(entry, &aProc)
44
+ rescue Errno::ENOENT
45
+ end
46
+ }
47
+ }
48
+ raise Errno::ENOENT,
49
+ "No matching entry found in zip files '#{@zipFileList.join(', ')}' "+
50
+ " for '#{entry}'"
51
+ end
52
+ end
53
+
54
+
55
+ module Kernel #:nodoc:all
56
+ alias :oldRequire :require
57
+
58
+ def require(moduleName)
59
+ zip_require(moduleName) || oldRequire(moduleName)
60
+ end
61
+
62
+ def zip_require(moduleName)
63
+ return false if already_loaded?(moduleName)
64
+ get_resource(ensure_rb_extension(moduleName)) {
65
+ |zis|
66
+ eval(zis.read); $" << moduleName
67
+ }
68
+ return true
69
+ rescue Errno::ENOENT => ex
70
+ return false
71
+ end
72
+
73
+ def get_resource(resourceName, &aProc)
74
+ zl = ZipList.new($:.grep(/\.zip$/))
75
+ zl.get_input_stream(resourceName, &aProc)
76
+ end
77
+
78
+ def already_loaded?(moduleName)
79
+ moduleRE = Regexp.new("^"+moduleName+"(\.rb|\.so|\.dll|\.o)?$")
80
+ $".detect { |e| e =~ moduleRE } != nil
81
+ end
82
+
83
+ def ensure_rb_extension(aString)
84
+ aString.sub(/(\.rb)?$/i, ".rb")
85
+ end
86
+ end
87
+
88
+ # Copyright (C) 2002 Thomas Sondergaard
89
+ # rubyzip is free software; you can redistribute it and/or
90
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << "../lib"
4
+ system("zip example.zip example.rb gtkRubyzip.rb")
5
+
6
+ require 'zip/zip'
7
+
8
+ ####### Using ZipInputStream alone: #######
9
+
10
+ Zip::ZipInputStream.open("example.zip") {
11
+ |zis|
12
+ entry = zis.get_next_entry
13
+ print "First line of '#{entry.name} (#{entry.size} bytes): "
14
+ puts "'#{zis.gets.chomp}'"
15
+ entry = zis.get_next_entry
16
+ print "First line of '#{entry.name} (#{entry.size} bytes): "
17
+ puts "'#{zis.gets.chomp}'"
18
+ }
19
+
20
+
21
+ ####### Using ZipFile to read the directory of a zip file: #######
22
+
23
+ zf = Zip::ZipFile.new("example.zip")
24
+ zf.each_with_index {
25
+ |entry, index|
26
+
27
+ puts "entry #{index} is #{entry.name}, size = #{entry.size}, compressed size = #{entry.compressed_size}"
28
+ # use zf.get_input_stream(entry) to get a ZipInputStream for the entry
29
+ # entry can be the ZipEntry object or any object which has a to_s method that
30
+ # returns the name of the entry.
31
+ }
32
+
33
+
34
+ ####### Using ZipOutputStream to write a zip file: #######
35
+
36
+ Zip::ZipOutputStream.open("exampleout.zip") {
37
+ |zos|
38
+ zos.put_next_entry("the first little entry")
39
+ zos.puts "Hello hello hello hello hello hello hello hello hello"
40
+
41
+ zos.put_next_entry("the second little entry")
42
+ zos.puts "Hello again"
43
+
44
+ # Use rubyzip or your zip client of choice to verify
45
+ # the contents of exampleout.zip
46
+ }
47
+
48
+ ####### Using ZipFile to change a zip file: #######
49
+
50
+ Zip::ZipFile.open("exampleout.zip") {
51
+ |zf|
52
+ zf.add("thisFile.rb", "example.rb")
53
+ zf.rename("thisFile.rb", "ILikeThisName.rb")
54
+ zf.add("Again", "example.rb")
55
+ }
56
+
57
+ # Lets check
58
+ Zip::ZipFile.open("exampleout.zip") {
59
+ |zf|
60
+ puts "Changed zip file contains: #{zf.entries.join(', ')}"
61
+ zf.remove("Again")
62
+ puts "Without 'Again': #{zf.entries.join(', ')}"
63
+ }
64
+
65
+ # For other examples, look at zip.rb and ziptest.rb
66
+
67
+ # Copyright (C) 2002 Thomas Sondergaard
68
+ # rubyzip is free software; you can redistribute it and/or
69
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << "../lib"
4
+
5
+ require 'zip/zipfilesystem'
6
+
7
+ EXAMPLE_ZIP = "filesystem.zip"
8
+
9
+ File.delete(EXAMPLE_ZIP) if File.exists?(EXAMPLE_ZIP)
10
+
11
+ Zip::ZipFile.open(EXAMPLE_ZIP, Zip::ZipFile::CREATE) {
12
+ |zf|
13
+ zf.file.open("file1.txt", "w") { |os| os.write "first file1.txt" }
14
+ zf.dir.mkdir("dir1")
15
+ zf.dir.chdir("dir1")
16
+ zf.file.open("file1.txt", "w") { |os| os.write "second file1.txt" }
17
+ puts zf.file.read("file1.txt")
18
+ puts zf.file.read("../file1.txt")
19
+ zf.dir.chdir("..")
20
+ zf.file.open("file2.txt", "w") { |os| os.write "first file2.txt" }
21
+ puts "Entries: #{zf.entries.join(', ')}"
22
+ }
23
+
24
+ Zip::ZipFile.open(EXAMPLE_ZIP) {
25
+ |zf|
26
+ puts "Entries from reloaded zip: #{zf.entries.join(', ')}"
27
+ }
28
+
29
+ # For other examples, look at zip.rb and ziptest.rb
30
+
31
+ # Copyright (C) 2003 Thomas Sondergaard
32
+ # rubyzip is free software; you can redistribute it and/or
33
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << "../lib"
4
+
5
+ $VERBOSE = true
6
+
7
+ require 'gtk'
8
+ require 'zip/zip'
9
+
10
+ class MainApp < Gtk::Window
11
+ def initialize
12
+ super()
13
+ set_usize(400, 256)
14
+ set_title("rubyzip")
15
+ signal_connect(Gtk::Window::SIGNAL_DESTROY) { Gtk.main_quit }
16
+
17
+ box = Gtk::VBox.new(false, 0)
18
+ add(box)
19
+
20
+ @zipfile = nil
21
+ @buttonPanel = ButtonPanel.new
22
+ @buttonPanel.openButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
23
+ show_file_selector
24
+ }
25
+ @buttonPanel.extractButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
26
+ puts "Not implemented!"
27
+ }
28
+ box.pack_start(@buttonPanel, false, false, 0)
29
+
30
+ sw = Gtk::ScrolledWindow.new
31
+ sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
32
+ box.pack_start(sw, true, true, 0)
33
+
34
+ @clist = Gtk::CList.new(["Name", "Size", "Compression"])
35
+ @clist.set_selection_mode(Gtk::SELECTION_BROWSE)
36
+ @clist.set_column_width(0, 120)
37
+ @clist.set_column_width(1, 120)
38
+ @clist.signal_connect(Gtk::CList::SIGNAL_SELECT_ROW) {
39
+ |w, row, column, event|
40
+ @selected_row = row
41
+ }
42
+ sw.add(@clist)
43
+ end
44
+
45
+ class ButtonPanel < Gtk::HButtonBox
46
+ attr_reader :openButton, :extractButton
47
+ def initialize
48
+ super
49
+ set_layout(Gtk::BUTTONBOX_START)
50
+ set_spacing(0)
51
+ @openButton = Gtk::Button.new("Open archive")
52
+ @extractButton = Gtk::Button.new("Extract entry")
53
+ pack_start(@openButton)
54
+ pack_start(@extractButton)
55
+ end
56
+ end
57
+
58
+ def show_file_selector
59
+ @fileSelector = Gtk::FileSelection.new("Open zip file")
60
+ @fileSelector.show
61
+ @fileSelector.ok_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
62
+ open_zip(@fileSelector.filename)
63
+ @fileSelector.destroy
64
+ }
65
+ @fileSelector.cancel_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
66
+ @fileSelector.destroy
67
+ }
68
+ end
69
+
70
+ def open_zip(filename)
71
+ @zipfile = Zip::ZipFile.open(filename)
72
+ @clist.clear
73
+ @zipfile.each {
74
+ |entry|
75
+ @clist.append([ entry.name,
76
+ entry.size.to_s,
77
+ (100.0*entry.compressedSize/entry.size).to_s+"%" ])
78
+ }
79
+ end
80
+ end
81
+
82
+ mainApp = MainApp.new()
83
+
84
+ mainApp.show_all
85
+
86
+ Gtk.main
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE=true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'Qt'
8
+ system('rbuic -o zipdialogui.rb zipdialogui.ui')
9
+ require 'zipdialogui.rb'
10
+ require 'zip/zip'
11
+
12
+
13
+
14
+ a = Qt::Application.new(ARGV)
15
+
16
+ class ZipDialog < ZipDialogUI
17
+
18
+
19
+ def initialize()
20
+ super()
21
+ connect(child('add_button'), SIGNAL('clicked()'),
22
+ self, SLOT('add_files()'))
23
+ connect(child('extract_button'), SIGNAL('clicked()'),
24
+ self, SLOT('extract_files()'))
25
+ end
26
+
27
+ def zipfile(&proc)
28
+ Zip::ZipFile.open(@zip_filename, &proc)
29
+ end
30
+
31
+ def each(&proc)
32
+ Zip::ZipFile.foreach(@zip_filename, &proc)
33
+ end
34
+
35
+ def refresh()
36
+ lv = child("entry_list_view")
37
+ lv.clear
38
+ each {
39
+ |e|
40
+ lv.insert_item(Qt::ListViewItem.new(lv, e.name, e.size.to_s))
41
+ }
42
+ end
43
+
44
+
45
+ def load(zipfile)
46
+ @zip_filename = zipfile
47
+ refresh
48
+ end
49
+
50
+ def add_files
51
+ l = Qt::FileDialog.getOpenFileNames(nil, nil, self)
52
+ zipfile {
53
+ |zf|
54
+ l.each {
55
+ |path|
56
+ zf.add(File.basename(path), path)
57
+ }
58
+ }
59
+ refresh
60
+ end
61
+
62
+ def extract_files
63
+ selected_items = []
64
+ unselected_items = []
65
+ lv_item = entry_list_view.first_child
66
+ while (lv_item)
67
+ if entry_list_view.is_selected(lv_item)
68
+ selected_items << lv_item.text(0)
69
+ else
70
+ unselected_items << lv_item.text(0)
71
+ end
72
+ lv_item = lv_item.next_sibling
73
+ end
74
+ puts "selected_items.size = #{selected_items.size}"
75
+ puts "unselected_items.size = #{unselected_items.size}"
76
+ items = selected_items.size > 0 ? selected_items : unselected_items
77
+ puts "items.size = #{items.size}"
78
+
79
+ d = Qt::FileDialog.get_existing_directory(nil, self)
80
+ if (!d)
81
+ puts "No directory chosen"
82
+ else
83
+ zipfile { |zf| items.each { |e| zf.extract(e, File.join(d, e)) } }
84
+ end
85
+
86
+ end
87
+
88
+ slots 'add_files()', 'extract_files()'
89
+ end
90
+
91
+ if !ARGV[0]
92
+ puts "usage: #{$0} zipname"
93
+ exit
94
+ end
95
+
96
+ zd = ZipDialog.new
97
+ zd.load(ARGV[0])
98
+
99
+ a.mainWidget = zd
100
+ zd.show()
101
+ a.exec()
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << "../lib"
4
+
5
+ require 'zip/zip'
6
+
7
+ include Zip
8
+
9
+ ZipOutputStream.open('simple.zip') {
10
+ |zos|
11
+ ze = zos.put_next_entry 'entry.txt'
12
+ zos.puts "Hello world"
13
+ }
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'zip/zip'
8
+ require 'find'
9
+
10
+ module Zip
11
+ module ZipFind
12
+ def self.find(path, zipFilePattern = /\.zip$/i)
13
+ Find.find(path) {
14
+ |fileName|
15
+ yield(fileName)
16
+ if zipFilePattern.match(fileName) && File.file?(fileName)
17
+ begin
18
+ Zip::ZipFile.foreach(fileName) {
19
+ |zipEntry|
20
+ yield(fileName + File::SEPARATOR + zipEntry.to_s)
21
+ }
22
+ rescue Errno::EACCES => ex
23
+ puts ex
24
+ end
25
+ end
26
+ }
27
+ end
28
+
29
+ def self.find_file(path, fileNamePattern, zipFilePattern = /\.zip$/i)
30
+ self.find(path, zipFilePattern) {
31
+ |fileName|
32
+ yield(fileName) if fileNamePattern.match(fileName)
33
+ }
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ if __FILE__ == $0
40
+ module ZipFindConsoleRunner
41
+
42
+ PATH_ARG_INDEX = 0;
43
+ FILENAME_PATTERN_ARG_INDEX = 1;
44
+ ZIPFILE_PATTERN_ARG_INDEX = 2;
45
+
46
+ def self.run(args)
47
+ check_args(args)
48
+ Zip::ZipFind.find_file(args[PATH_ARG_INDEX],
49
+ args[FILENAME_PATTERN_ARG_INDEX],
50
+ args[ZIPFILE_PATTERN_ARG_INDEX]) {
51
+ |fileName|
52
+ report_entry_found fileName
53
+ }
54
+ end
55
+
56
+ def self.check_args(args)
57
+ if (args.size != 3)
58
+ usage
59
+ exit
60
+ end
61
+ end
62
+
63
+ def self.usage
64
+ puts "Usage: #{$0} PATH ZIPFILENAME_PATTERN FILNAME_PATTERN"
65
+ end
66
+
67
+ def self.report_entry_found(fileName)
68
+ puts fileName
69
+ end
70
+
71
+ end
72
+
73
+ ZipFindConsoleRunner.run(ARGV)
74
+ end