OSDN Git Service

dump zip on scrap
authornomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 18:57:29 +0000 (03:57 +0900)
committernomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 18:57:29 +0000 (03:57 +0900)
bin/load-entries.rb

index 849fa3e..271ea17 100644 (file)
@@ -7,6 +7,7 @@ require 'arc'
 require 'pmd'
 require 'vmd'
 require 'x'
+require 'scrap'
 
 location_code = ARGV.shift || 'mmd'
 location = Location.find_by_code(location_code)
@@ -14,15 +15,20 @@ unless location
   puts "location not found."
   exit
 end
+scrap = Scrap.find_by_location(location.code)
 
-while line = gets
-  case line
-  when /^$/
-    #
-  when /^error:/
-    #
-  when /^# (.+)/
-    path = $1
+require 'rubygems'
+gem 'zipruby'
+require 'zipruby'
+require 'nkf'
+
+def encode(str)
+  str && NKF.nkf('-Sw', str)
+end
+
+def dump_zip(path, location)
+  Zip::Archive.open(path) do |ar|
+    puts "# #{path}"
     basename = File.basename(path)
     md = /\.(.+)\z/.match(basename)
     extname = md[1]
@@ -38,19 +44,42 @@ while line = gets
       code = code[3..-1]
     end
     arc = Arc.find_by_location_id_and_code(location.id, code)
-  else
-    size, comp_size, date, path = line.chomp.split(/\t/)
-    if arc && /\.pmd\z/.match(path)
-      puts [ code, path ].join(' ')
-      arc.pmds.find_or_create_by_path(path)
-    end
-    if arc && /\.vmd\z/.match(path)
-      puts [ code, path ].join(' ')
-      arc.vmds.find_or_create_by_path(path)
+
+    n = ar.num_files # number of entries
+    n.times do |i|
+      entry_name = ar.get_name(i) # get entry name from archive
+
+      # open entry
+      ar.fopen(i) do |f|
+        size, comp_size, date, path = f.size, f.comp_size, f.mtime.strftime("%d-%m-%y %H:%M"), encode(f.name)
+        if arc && /\.pmd\z/.match(path)
+          puts [ code, path ].join(' ')
+          arc.pmds.find_or_create_by_path(path)
+        end
+        if arc && /\.vmd\z/.match(path)
+          puts [ code, path ].join(' ')
+          arc.vmds.find_or_create_by_path(path)
+        end
+        if arc && /\.x\z/.match(path)
+          puts [ code, path ].join(' ')
+          arc.xes.find_or_create_by_path(path)
+        end
+
+        # content = f.read # read entry content
+      end
     end
-    if arc && /\.x\z/.match(path)
-      puts [ code, path ].join(' ')
-      arc.xes.find_or_create_by_path(path)
+  end
+rescue Zip::Error => exc
+  puts "error: " + exc.message
+end
+
+ent = "/Volumes/uploader/src/#{location.code}/index.html"
+open(ent) do |f|
+  while line = f.gets
+    if scrap.match(line)
+      name, = scrap.row
+      dump_zip("/Volumes/uploader/arc/#{location.code}/#{name}", location)
+      puts
     end
   end
 end