lib/real_zip.rb in real_zip-0.0.8 vs lib/real_zip.rb in real_zip-0.2.0
- old
+ new
@@ -1,9 +1,11 @@
require "real_zip/version"
+
require 'yaml'
require 'forwardable'
require 'zip/zipfilesystem'
+
def RealZip file, structure
RealZip.new file, structure
end
module RealZip
@@ -30,54 +32,45 @@
def collect_all(given)
found = []
traverse given do |x,y| found << [x,y] end
found
end
-
def collect_only(given, kind)
collect_all(given).map { |(name,type)| name if type == kind }.compact
- end
-
+ end
def files(given)
collect_only(given, :file).map { |x| x.join ?/ }
- var = File.open(given)
-
end
-
def dirs(given)
collect_only(given, :dir).map { |x| x.join ?/ }
- end
-end
+ end
+ end
-
-
####################
- class RealZip < Struct.new :file_structure
+ class RealZip < Struct.new :file_structure
def save file
File.delete file if File.exist? file
Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
+ # first_dir = nil
dirs(struct).each do |dir|
+ first_dir = dir
z.dir.mkdir(dir) unless z.file.exist? dir
end
- files(struct).each do |file|
- z.file.open(file, "w") { |f| f.write File.new(file, "r") }
- end
+ op1 = struct
+ arr = op1.values.first
+ # op2 = files(op1)
+ # res = op1.to_s + " ==== " + op2.to_s
+ files(op1).each_with_index do |file,index|
+ file_path = arr[index]
+ # FileUtils.cp("/home/raw/Code/prasad/zip.rake", file)
+ string = File.open(file_path, 'r'){ |f| f.read }
+ z.file.open(file, "w") { |f| f.write string }
+ end
end
end
- # def save file
- # File.delete file if File.exist? file
- # Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
- # dirs(struct).each do |dir|
- # z.dir.mkdir(dir) unless z.file.exist? dir
- # end
- # files(struct).each do |file|
- # z.file.open(file, "w") { |f| f.write file }
- # end
- # end
-
def struct given=file_structure
given.is_a?(String) ? YAML.load(given) : given
end
private
@@ -93,18 +86,10 @@
module_function :new
end
__END__
- # Zippy.create 'my.zip' do |z|
- # add_dir z, 'my', 'app'
- # add_dir z, 'my', 'config'
- # add_file z, 'my', 'config.ru'
- # add_file z, 'my', 'Gemfile'
- # end
-
-
# Other tests...
found = []
RealZip::Helpers.traverse( {a: [{a: 1, b: 2}, 3]}, &proc{ |x| found << x } )
found.map { |x| x.join ?/ } == ["a", "a/a", "a/a/1", "a/b", "a/b/2", "a/3"] or raise
@@ -128,23 +113,6 @@
z.file.exist? 'root_dir/file1' or raise
z.file.exist? 'root_dir/file2' or raise
z.file.exist? 'root_dir/nested_dir/nested_file.any' or raise
z.file.exist? 'root_dir/empty_dir' or raise # no empty dirs
end
-File.delete file_name
-
-
- # def add_file( zippyfile, dst_dir, f )
- # zippyfile["#{dst_dir}/#{f}"] = File.open(f)
- # if File.file?(f)
- # FileUtils.copy_file f, dst_dir
- # end
- # end
-
- # def add_dir( zippyfile, dst_dir, d )
- # glob = "#{d}/**/*"
- # FileList.new( glob ).each { |f|
- # if (File.file?(f))
- # add_file zippyfile, dst_dir, f
- # end
- # }
- # end
+File.delete file_name
\ No newline at end of file