| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | #!/usr/bin/env ruby | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-17 01:47:44 +08:00
										 |  |  | $: << "../lib" | 
					
						
							| 
									
										
										
										
											2002-03-30 06:26:20 +08:00
										 |  |  | system("zip example.zip example.rb gtkRubyzip.rb") | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-08-21 21:54:32 +08:00
										 |  |  | require 'zip/zip' | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | ####### Using ZipInputStream alone: ####### | 
					
						
							| 
									
										
										
										
											2002-01-05 08:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::InputStream.open("example.zip") { | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  |   |zis| | 
					
						
							| 
									
										
										
										
											2003-08-13 22:29:53 +08:00
										 |  |  |   entry = zis.get_next_entry | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  |   print "First line of '#{entry.name} (#{entry.size} bytes):  " | 
					
						
							|  |  |  |   puts "'#{zis.gets.chomp}'" | 
					
						
							| 
									
										
										
										
											2003-08-13 22:29:53 +08:00
										 |  |  |   entry = zis.get_next_entry | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  |   print "First line of '#{entry.name} (#{entry.size} bytes):  " | 
					
						
							|  |  |  |   puts "'#{zis.gets.chomp}'" | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-05 08:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  | ####### Using ZipFile to read the directory of a zip file: ####### | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | zf = Zip::File.new("example.zip") | 
					
						
							| 
									
										
										
										
											2002-01-05 08:52:47 +08:00
										 |  |  | zf.each_with_index { | 
					
						
							|  |  |  |   |entry, index| | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2003-08-19 02:30:26 +08:00
										 |  |  |   puts "entry #{index} is #{entry.name}, size = #{entry.size}, compressed size = #{entry.compressed_size}" | 
					
						
							| 
									
										
										
										
											2003-08-13 22:29:53 +08:00
										 |  |  |   # use zf.get_input_stream(entry) to get a ZipInputStream for the entry | 
					
						
							| 
									
										
										
										
											2002-01-05 08:52:47 +08:00
										 |  |  |   # entry can be the ZipEntry object or any object which has a to_s method that | 
					
						
							|  |  |  |   # returns the name of the entry. | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | ####### Using ZipOutputStream to write a zip file: ####### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::OutputStream.open("exampleout.zip") { | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  |   |zos| | 
					
						
							| 
									
										
										
										
											2003-08-13 22:29:53 +08:00
										 |  |  |   zos.put_next_entry("the first little entry") | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  |   zos.puts "Hello hello hello hello hello hello hello hello hello" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-08-13 22:29:53 +08:00
										 |  |  |   zos.put_next_entry("the second little entry") | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  |   zos.puts "Hello again" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use rubyzip or your zip client of choice to verify | 
					
						
							|  |  |  |   # the contents of exampleout.zip | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  | ####### Using ZipFile to change a zip file: ####### | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::File.open("exampleout.zip") { | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  |   |zf| | 
					
						
							|  |  |  |   zf.add("thisFile.rb", "example.rb") | 
					
						
							|  |  |  |   zf.rename("thisFile.rb", "ILikeThisName.rb") | 
					
						
							|  |  |  |   zf.add("Again", "example.rb") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Lets check | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::File.open("exampleout.zip") { | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  |   |zf| | 
					
						
							|  |  |  |   puts "Changed zip file contains: #{zf.entries.join(', ')}" | 
					
						
							|  |  |  |   zf.remove("Again") | 
					
						
							|  |  |  |   puts "Without 'Again': #{zf.entries.join(', ')}" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  | ####### Using ZipFile to split a zip file: ####### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Creating large zip file for splitting | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::OutputStream.open("large_zip_file.zip") do |zos| | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  |   puts "Creating zip file..." | 
					
						
							|  |  |  |   10.times do |i| | 
					
						
							|  |  |  |     zos.put_next_entry("large_entry_#{i}.txt") | 
					
						
							|  |  |  |     zos.puts "Hello" * 104857600
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Splitting created large zip file | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | part_zips_count = Zip::File.split("large_zip_file.zip", 2097152, false) | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  | puts "Zip file splitted in #{part_zips_count} parts" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Track splitting an archive | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:24 +08:00
										 |  |  | Zip::File.split("large_zip_file.zip", 1048576, true, 'part_zip_file') do | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  |   |part_count, part_index, chunk_bytes, segment_bytes| | 
					
						
							|  |  |  |   puts "#{part_index} of #{part_count} part splitting: #{(chunk_bytes.to_f/segment_bytes.to_f * 100).to_i}%" | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | # For other examples, look at zip.rb and ziptest.rb | 
					
						
							| 
									
										
										
										
											2002-02-02 22:58:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Copyright (C) 2002 Thomas Sondergaard | 
					
						
							|  |  |  | # rubyzip is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the ruby license. |