removing ferret cruft
[zip-doc.git] / zdump.rb
blob960d73f486d5553bab78fe2098d6319c625e3b74
1 #!/usr/bin/ruby
2 %w(md5 zcompress find htmlshrinker zcompress).each {|x| require x}
3          
4 def unpack(string)
5   return string.unpack('H32V4' * (string.size/32))
6 end  
7   
8 def pack(md5, bstart, bsize, start, size)
9   return [md5, bstart, bsize, start, size].pack('H32V4')
10 end
12 def md5subset(four)
13   sprintf("%d", "0x" + four[0..3]).to_i                                                  
14 end
15           
16 HTMLSHRINKER = HTMLShrinker.new(ARGV[1])
18 class Webpage    
19   attr_reader :text, :compressed, :size, :compressed_size, :filename, :index_content, :block, :buflocation
20   
21   def initialize(filename, block, buflocation)
22     @filename = filename                                                                    
23     @block = block
24     @text = HTMLSHRINKER.compress(File.read(filename))
25     @size = @text.size
26 #    @index_content = index_content           
27     @buflocation = buflocation
28   end
29                     
30   def empty!
31     @text = ''
32     @index_content = ''
33   end
34   
35   def index_content
36     content = ""
37     case @filename
38       when /.txt$/i
39         content = @text
41       when /.htm$|.html$/i        # get the file, strip all <> tags
42         content = @text.gsub(/\<head>.*?\<\/head>/im,"").gsub(/\<.*?\>/m, " ")
43     end
44     return content.strip
45   end    
46 end
47             
48 class Block                         
49   attr_reader :number, :start, :size
50   def initialize(number, start, size)  
51     @number = number
52     @start = start
53     @size = size
54   end
55 end
56                                                    
57 index = []             
58 block_ary = []
59 cur_block, counter, buflocation, size, buffer = 0, 0, 0, 0, ""
60 location = 4 # (to hold start of index)
62 name = (ARGV[1] ? ARGV[1] : "default")
63             
64 t = Time.now
65 puts "Indexing files in #{ARGV[0]}/ and writing the file #{name}.zindex and directory #{name}.zferret."
66 zdump = File.open("#{name}.zdump", "w")
67 zdump.seek(location)
69 ignore = ARGV[2] ? Regexp.new(ARGV[2]) : /^(Bilde~|Bruker|Pembicaraan_Pengguna~)/ 
71 Find.find(ARGV[0]) do |newfile|
72   next if File.directory?(newfile) || !File.readable?(newfile)
73   next if newfile =~ ignore
74   wf = Webpage.new(newfile, cur_block, buflocation)
75   puts "#{counter} files indexed." if counter.to_i / 100.0 == counter / 100
77   buffer << wf.text
78   buflocation += wf.text.size
79   wf.empty!
80   counter += 1                  
81   index << wf
82   next if buffer.size < 900000
84   bf_compr = ZCompress::compress(buffer)
85   zdump.write(bf_compr)
86   block_ary[cur_block] = Block.new(cur_block, location, bf_compr.size)
87   buffer = ''       
88   buflocation = 0
89   cur_block += 1                                           
90   location += bf_compr.size
91   puts "Writing block no #{cur_block}"
92        
93 #  ZFERRET << {:filename => wf.filename, :content => wf.index_content, :offset => location, :size => wf.compressed_size } 
94 #  location += wf.compressed_size
96 end        
98 # to ensure last part of buffer is written
99 bf_compr = ZCompress::compress(buffer)
100 zdump.write(bf_compr)
101 block_ary[cur_block] = Block.new(cur_block, location, bf_compr.size)
102 location += bf_compr.size                             
104 # writing start of index
105 zdump.seek(0)          
106 zdump.write([location].pack('V'))                      
107 puts "location #{location}"
108 puts "Finished, writing index. #{Time.now - t}"
109            
110 pages = {}
111 index.each do |file|
112   pages[file.filename] = {:block_start => block_ary[file.block].start,
113                           :block_size => block_ary[file.block].size,
114                           :start => file.buflocation,
115                           :size => file.size}         
117 subindex = []                        
119 puts "Sorted onetime. #{Time.now - t}"
120 pages.each_pair do |x, y| 
121   md5 = MD5.md5(x).hexdigest
122   entry = pack(md5, y[:block_start], y[:block_size], y[:start], y[:size])
123   firstfour = md5subset(md5)
124   subindex[firstfour] = "" if subindex[firstfour].nil?
125   subindex[firstfour] << entry
128 puts "Sorted another time. #{Time.now - t}"
129 indexloc = location
130 location = (65535*8) + indexloc
131  p = File.open(name + ".zlog",'w')
132 subindex.each_with_index do |entry, idx|
133   next if entry.nil?  
134   zdump.seek((idx*8) + indexloc)                   
135   zdump.print([location, entry.size].pack('V2'))
136   zdump.seek(location)
137   zdump.print(entry)         
139    p << "*" * 80 << "\n" 
140    p << "seek #{(idx*8) + location} location #{location} size #{entry.size}" << "\n"
141    p << unpack(entry).join(":") << "\n"
143   location += entry.size
145 puts "Finished. #{Time.now - t}"
146 zdump.close
147 # p.close