2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								#!/usr/bin/env ruby 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$VERBOSE  =  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								class  TestFiles 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  RANDOM_ASCII_FILE1   =  " test/data/generated/randomAscii1.txt " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  RANDOM_ASCII_FILE2   =  " test/data/generated/randomAscii2.txt " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  RANDOM_ASCII_FILE3   =  " test/data/generated/randomAscii3.txt " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  RANDOM_BINARY_FILE1  =  " test/data/generated/randomBinary1.bin " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  RANDOM_BINARY_FILE2  =  " test/data/generated/randomBinary2.bin " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  EMPTY_TEST_DIR  =  " test/data/generated/emptytestdir " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  ASCII_TEST_FILES  =  [ RANDOM_ASCII_FILE1 ,  RANDOM_ASCII_FILE2 ,  RANDOM_ASCII_FILE3 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  BINARY_TEST_FILES  =  [ RANDOM_BINARY_FILE1 ,  RANDOM_BINARY_FILE2 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  TEST_DIRECTORIES  =  [ EMPTY_TEST_DIR ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  TEST_FILES  =  [ ASCII_TEST_FILES ,  BINARY_TEST_FILES ,  EMPTY_TEST_DIR ] . flatten! 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  class  <<  self 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    def  create_test_files 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        Dir . mkdir  " test/data/generated "  unless  Dir . exist? ( 'test/data/generated' ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        ASCII_TEST_FILES . each_with_index  do  | filename ,  index | 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          create_random_ascii ( filename ,  1 E4  *  ( index + 1 ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        BINARY_TEST_FILES . each_with_index  do  | filename ,  index | 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          create_random_binary ( filename ,  1 E4  *  ( index + 1 ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        ensure_dir ( EMPTY_TEST_DIR ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    private 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    def  create_random_ascii ( filename ,  size ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      File . open ( filename ,  " wb " )  do  | file | 
							 
						 
					
						
							
								
									
										
										
										
											2015-03-20 23:33:43 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        file  <<  rand  while  ( file . tell  <  size ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								      end 
							 
						 
					
						
							
								
									
										
										
										
											2012-02-06 08:59:11 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    def  create_random_binary ( filename ,  size ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      File . open ( filename ,  " wb " )  do  | file | 
							 
						 
					
						
							
								
									
										
										
										
											2015-03-20 23:33:43 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        file  <<  [ rand ] . pack ( " V " )  while  ( file . tell  <  size ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								      end 
							 
						 
					
						
							
								
									
										
										
										
											2012-02-06 08:59:11 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    def  ensure_dir ( name ) 
							 
						 
					
						
							
								
									
										
										
										
											2014-02-07 07:00:38 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      if  File . exist? ( name ) 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        return  if  File . stat ( name ) . directory? 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        File . delete ( name ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      Dir . mkdir ( name ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  end 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# For representation and creation of 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# test data 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								class  TestZipFile 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  attr_accessor  :zip_name ,  :entry_names ,  :comment 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  def  initialize ( zip_name ,  entry_names ,  comment  =  " " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    @zip_name = zip_name 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    @entry_names = entry_names 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    @comment  =  comment 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  def  TestZipFile . create_test_zips 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    raise  " failed to create test zip ' #{ TEST_ZIP1 . zip_name } ' "  unless  system ( " /usr/bin/zip  #{ TEST_ZIP1 . zip_name }  test/data/file2.txt " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    raise  " failed to remove entry from ' #{ TEST_ZIP1 . zip_name } ' "  unless  system ( " /usr/bin/zip  #{ TEST_ZIP1 . zip_name }  -d test/data/file2.txt " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/empty.txt " ,  " w " )  { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/empty_chmod640.txt " ,  " w " )  { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    :: File . chmod ( 0640 ,  " test/data/generated/empty_chmod640.txt " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/short.txt " ,  " w " )  {  | file |  file  <<  " ABCDEF "  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    ziptestTxt = " " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/file2.txt " )  {  | file |  ziptestTxt = file . read  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/longAscii.txt " ,  " w " )  do  | file | 
							 
						 
					
						
							
								
									
										
										
										
											2015-03-20 23:33:43 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      file  <<  ziptestTxt  while  ( file . tell  <  1 E5 ) 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    testBinaryPattern = " " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/empty.zip " )  {  | file |  testBinaryPattern = file . read  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    testBinaryPattern  *=  4 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    File . open ( " test/data/generated/longBinary.bin " ,  " wb " )  do  | file | 
							 
						 
					
						
							
								
									
										
										
										
											2015-03-20 23:33:43 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      file  <<  testBinaryPattern  <<  rand  <<  " \0 "  while  ( file . tell  <  6 E5 ) 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    raise  " failed to create test zip ' #{ TEST_ZIP2 . zip_name } ' "  unless  system ( " /usr/bin/zip  #{ TEST_ZIP2 . zip_name }   #{ TEST_ZIP2 . entry_names . join ( ' ' ) } " ) 
							 
						 
					
						
							
								
									
										
										
										
											2012-02-06 08:59:11 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    if  RUBY_PLATFORM  =~  / mswin|mingw|cygwin / 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      raise  " failed to add comment to test zip ' #{ TEST_ZIP2 . zip_name } ' "  unless  system ( " echo  #{ TEST_ZIP2 . comment } | /usr/bin/zip -z  #{ TEST_ZIP2 . zip_name } \" " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    else 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								      # without bash system interprets everything after echo as parameters to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      # echo including | zip -z ... 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      raise  " failed to add comment to test zip ' #{ TEST_ZIP2 . zip_name } ' "  unless  system ( " bash -c  \" echo  #{ TEST_ZIP2 . comment }  | /usr/bin/zip -z  #{ TEST_ZIP2 . zip_name } \" " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    end 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    raise  " failed to create test zip ' #{ TEST_ZIP3 . zip_name } ' "  unless  system ( " /usr/bin/zip  #{ TEST_ZIP3 . zip_name }   #{ TEST_ZIP3 . entry_names . join ( ' ' ) } " ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    raise  " failed to create test zip ' #{ TEST_ZIP4 . zip_name } ' "  unless  system ( " /usr/bin/zip  #{ TEST_ZIP4 . zip_name }   #{ TEST_ZIP4 . entry_names . join ( ' ' ) } " ) 
							 
						 
					
						
							
								
									
										
										
										
											2012-02-06 08:59:11 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  rescue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    # If there are any Windows developers wanting to use a command line zip.exe 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    # to help create the following files, there's a free one available from 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    # http://stahlworks.com/dev/index.php?tool=zipunzip 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    # that works with the above code 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    raise  $! . to_s  + 
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								              " \n \n ziptest.rb requires the Info-ZIP program 'zip' in the path \n "  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              " to create test data. If you don't have it you can download \n "  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              " the necessary test files at http://sf.net/projects/rubyzip. " 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  end 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2014-01-21 05:31:06 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  TEST_ZIP1  =  TestZipFile . new ( " test/data/generated/empty.zip " ,  [ ] ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  TEST_ZIP2  =  TestZipFile . new ( " test/data/generated/5entry.zip " ,  %w{  test/data/generated/longAscii.txt test/data/generated/empty.txt test/data/generated/empty_chmod640.txt test/data/generated/short.txt test/data/generated/longBinary.bin } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                              " my zip comment " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  TEST_ZIP3  =  TestZipFile . new ( " test/data/generated/test1.zip " ,  %w{  test/data/file1.txt  } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  TEST_ZIP4  =  TestZipFile . new ( " test/data/generated/zipWithDir.zip " ,  [ " test/data/file1.txt " , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                                                                TestFiles :: EMPTY_TEST_DIR ] ) 
							 
						 
					
						
							
								
									
										
										
										
											2005-02-16 05:10:20 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								end