| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | #!/usr/bin/env ruby | 
					
						
							| 
									
										
										
										
											2021-05-24 01:24:22 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 04:46:00 +08:00
										 |  |  | $LOAD_PATH << '../lib' | 
					
						
							| 
									
										
										
										
											2015-03-25 00:23:24 +08:00
										 |  |  | system('zip example.zip example.rb gtk_ruby_zip.rb') | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 19:20:27 +08:00
										 |  |  | require '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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | Zip::InputStream.open('example.zip') do |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}'" | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2002-01-03 01:48:31 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | zf = Zip::File.new('example.zip') | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | zf.each_with_index do |entry, index| | 
					
						
							| 
									
										
										
										
											2021-06-26 05:31:34 +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. | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2002-01-05 08:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | ####### Using ZipOutputStream to write a zip file: ####### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | Zip::OutputStream.open('exampleout.zip') do |zos| | 
					
						
							|  |  |  |   zos.put_next_entry('the first little entry') | 
					
						
							|  |  |  |   zos.puts 'Hello hello hello hello hello hello hello hello hello' | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  |   zos.put_next_entry('the second little entry') | 
					
						
							|  |  |  |   zos.puts 'Hello again' | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Use rubyzip or your zip client of choice to verify | 
					
						
							|  |  |  |   # the contents of exampleout.zip | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  | ####### Using ZipFile to change a zip file: ####### | 
					
						
							| 
									
										
										
										
											2002-01-26 06:29:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | Zip::File.open('exampleout.zip') do |zip_file| | 
					
						
							|  |  |  |   zip_file.add('thisFile.rb', 'example.rb') | 
					
						
							|  |  |  |   zip_file.rename('thisFile.rb', 'ILikeThisName.rb') | 
					
						
							|  |  |  |   zip_file.add('Again', 'example.rb') | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2002-03-21 04:18:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Lets check | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | Zip::File.open('exampleout.zip') do |zip_file| | 
					
						
							| 
									
										
										
										
											2015-03-21 04:05:59 +08:00
										 |  |  |   puts "Changed zip file contains: #{zip_file.entries.join(', ')}" | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  |   zip_file.remove('Again') | 
					
						
							| 
									
										
										
										
											2015-03-21 04:05:59 +08:00
										 |  |  |   puts "Without 'Again': #{zip_file.entries.join(', ')}" | 
					
						
							| 
									
										
										
										
											2015-03-21 16:10:37 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2015-03-21 16:27:44 +08:00
										 |  |  | Zip::OutputStream.open('large_zip_file.zip') do |zos| | 
					
						
							|  |  |  |   puts 'Creating zip file...' | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  |   10.times do |i| | 
					
						
							|  |  |  |     zos.put_next_entry("large_entry_#{i}.txt") | 
					
						
							| 
									
										
										
										
											2015-03-24 00:23:04 +08:00
										 |  |  |     zos.puts 'Hello' * 104_857_600
 | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Splitting created large zip file | 
					
						
							| 
									
										
										
										
											2015-03-24 00:23:04 +08:00
										 |  |  | part_zips_count = Zip::File.split('large_zip_file.zip', 2_097_152, false) | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  | puts "Zip file splitted in #{part_zips_count} parts" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Track splitting an archive | 
					
						
							| 
									
										
										
										
											2021-06-26 05:31:34 +08:00
										 |  |  | Zip::File.split( | 
					
						
							|  |  |  |   'large_zip_file.zip', 1_048_576, true, 'part_zip_file' | 
					
						
							|  |  |  | ) do |part_count, part_index, chunk_bytes, segment_bytes| | 
					
						
							|  |  |  |   puts "#{part_index} of #{part_count} part splitting: " \ | 
					
						
							|  |  |  |        "#{(chunk_bytes.to_f / segment_bytes * 100).to_i}%" | 
					
						
							| 
									
										
										
										
											2013-04-07 13:16:18 +08:00
										 |  |  | 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. |