Class: Zip::ZipFile

Inherits:
ZipCentralDirectory show all
Includes:
ZipFileSystem
Defined in:
lib/zip/zip.rb,
lib/zip/zipfilesystem.rb

Constant Summary collapse

CREATE =
1

Constants inherited from ZipCentralDirectory

Zip::ZipCentralDirectory::END_OF_CENTRAL_DIRECTORY_SIGNATURE, Zip::ZipCentralDirectory::MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, Zip::ZipCentralDirectory::STATIC_EOCD_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ZipFileSystem

#dir, #file

Methods inherited from ZipCentralDirectory

#==, #each, #entries, #get_e_o_c_d, #read_central_directory_entries, #read_e_o_c_d, #read_from_stream, read_from_stream, #size, #write_to_stream

Methods included from Enumerable

#deep_clone, #deep_dup, #inject, #select_map

Constructor Details

#initialize(fileName, create = nil) ⇒ ZipFile

Returns a new instance of ZipFile.


866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'lib/zip/zip.rb', line 866

def initialize(fileName, create = nil)
  super()
  @name = fileName
  @comment = ""
  if (File.exists?(fileName))
	File.open(name, "rb") { |f| read_from_stream(f) }
  elsif (create == ZipFile::CREATE)
	@entrySet = ZipEntrySet.new
  else
	raise ZipError, "File #{fileName} not found"
  end
  @create = create
  @storedEntries = @entrySet.dup
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment


894
895
896
# File 'lib/zip/zip.rb', line 894

def comment
  @comment
end

#nameObject (readonly)

Returns the value of attribute name


864
865
866
# File 'lib/zip/zip.rb', line 864

def name
  @name
end

Class Method Details

.foreach(aZipFileName, &block) ⇒ Object


896
897
898
899
900
901
# File 'lib/zip/zip.rb', line 896

def ZipFile.foreach(aZipFileName, &block)
  ZipFile.open(aZipFileName) {
	|zipFile|
	zipFile.each(&block)
  }
end

.open(fileName, create = nil) ⇒ Object


881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/zip/zip.rb', line 881

def ZipFile.open(fileName, create = nil)
  zf = ZipFile.new(fileName, create)
  if block_given?
	begin
	  yield zf
	ensure
	  zf.close
	end
  else
	zf
  end
end

Instance Method Details

#add(entry, srcPath, &continueOnExistsProc) ⇒ Object


926
927
928
929
930
931
932
933
934
935
# File 'lib/zip/zip.rb', line 926

def add(entry, srcPath, &continueOnExistsProc)
  continueOnExistsProc ||= proc { false }
  check_entry_exists(entry, continueOnExistsProc, "add")
  newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s)
  if is_directory(newEntry, srcPath)
	@entrySet << ZipStreamableDirectory.new(newEntry)
  else
	@entrySet << ZipStreamableFile.new(newEntry, srcPath)
  end
end

#closeObject


977
978
979
# File 'lib/zip/zip.rb', line 977

def close
  commit
end

#commitObject


962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/zip/zip.rb', line 962

def commit
 return if ! commit_required?
  on_success_replace(name) {
	|tmpFile|
	ZipOutputStream.open(tmpFile) {
	  |zos|

	  @entrySet.each { |e| e.write_to_zip_output_stream(zos) }
	  zos.comment = comment
	}
	true
  }
  initialize(name)
end

#commit_required?Boolean

Returns:

  • (Boolean)

981
982
983
# File 'lib/zip/zip.rb', line 981

def commit_required?
  return @entrySet != @storedEntries || @create == ZipFile::CREATE
end

#extract(entry, destPath, &onExistsProc) ⇒ Object


952
953
954
955
956
957
958
959
960
# File 'lib/zip/zip.rb', line 952

def extract(entry, destPath, &onExistsProc)
  onExistsProc ||= proc { false }
  foundEntry = get_entry(entry)
  if foundEntry.is_directory
	create_directory(foundEntry, destPath, &onExistsProc)
  else
	write_file(foundEntry, destPath, &onExistsProc) 
  end
end

#find_entry(entry) ⇒ Object


985
986
987
988
989
990
# File 'lib/zip/zip.rb', line 985

def find_entry(entry)
  @entrySet.detect { 
	|e| 
	e.name.sub(/\/$/, "") == entry.to_s.sub(/\/$/, "")
  }
end

#get_entry(entry) ⇒ Object


992
993
994
995
996
997
998
# File 'lib/zip/zip.rb', line 992

def get_entry(entry)
  selectedEntry = find_entry(entry)
  unless selectedEntry
	raise Errno::ENOENT, entry
  end
  return selectedEntry
end

#get_input_stream(entry, &aProc) ⇒ Object


903
904
905
# File 'lib/zip/zip.rb', line 903

def get_input_stream(entry, &aProc)
  get_entry(entry).get_input_stream(&aProc)
end

#get_output_stream(entry, &aProc) ⇒ Object


907
908
909
910
911
912
913
914
915
916
# File 'lib/zip/zip.rb', line 907

def get_output_stream(entry, &aProc)
  newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s)
  if newEntry.directory?
	raise ArgumentError,
	  "cannot open stream to directory entry - '#{newEntry}'"
  end
  zipStreamableEntry = ZipStreamableStream.new(newEntry)
  @entrySet << zipStreamableEntry
  zipStreamableEntry.get_output_stream(&aProc)      
end

#mkdir(entryName, permissionInt = 0) ⇒ Object

permissionInt ignored


1000
1001
1002
1003
1004
1005
# File 'lib/zip/zip.rb', line 1000

def mkdir(entryName, permissionInt = 0) #permissionInt ignored
  if find_entry(entryName)
    raise Errno::EEXIST, "File exists - #{entryName}"
  end
  @entrySet << ZipStreamableDirectory.new(ZipEntry.new(name, entryName.to_s.ensure_end("/")))
end

#read(entry) ⇒ Object


922
923
924
# File 'lib/zip/zip.rb', line 922

def read(entry)
  get_input_stream(entry) { |is| is.read } 
end

#remove(entry) ⇒ Object


937
938
939
# File 'lib/zip/zip.rb', line 937

def remove(entry)
  @entrySet.delete(get_entry(entry))
end

#rename(entry, newName, &continueOnExistsProc) ⇒ Object


941
942
943
944
945
# File 'lib/zip/zip.rb', line 941

def rename(entry, newName, &continueOnExistsProc)
  foundEntry = get_entry(entry)
  check_entry_exists(newName, continueOnExistsProc, "rename")
  foundEntry.name=newName
end

#replace(entry, srcPath) ⇒ Object


947
948
949
950
# File 'lib/zip/zip.rb', line 947

def replace(entry, srcPath)
  check_file(srcPath)
  add(remove(entry), srcPath)
end

#to_sObject


918
919
920
# File 'lib/zip/zip.rb', line 918

def to_s
  @name
end