Class: SwfUtil::SWFCompressor

Inherits:
SWFCompression show all
Defined in:
lib/swfheader/swf-compressor.rb

Instance Method Summary collapse

Methods inherited from SWFCompression

#is_compressed?, #read_full_size, #strip_header

Constructor Details

#initialize(from = nil, to = nil) ⇒ SWFCompressor



5
6
7
# File 'lib/swfheader/swf-compressor.rb', line 5

def initialize(from=nil,to=nil)
  read_file(from,to) if !from.nil? and !to.nil?
end

Instance Method Details

#compress(buffer) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/swfheader/swf-compressor.rb', line 18

def compress(buffer)
  compressor=Zlib::Deflate.new
  data=compressor.deflate(strip_header(buffer),Zlib::FINISH)
  data=buffer[0,8]+data
  data[0]=?C
  data
end

#read_file(file, to) ⇒ Object

Raises:

  • (RuntimeError.new)


8
9
10
11
12
13
14
15
16
17
# File 'lib/swfheader/swf-compressor.rb', line 8

def read_file(file,to)
  buff=File.open(file,"rb") do |fin|
    fin.read
  end
  raise RuntimeError.new,"The file have already been compressed",caller if is_compressed?(buff[0])
  result=compress(buff)
  File.open(to,"wb") do |fout|
    fout.write(result)
  end
end