Module: Swiff::Compression
- Included in:
- Swiff
- Defined in:
- lib/swiff/compression.rb
Instance Method Summary collapse
- #compress ⇒ Object
- #decompress ⇒ Object
- #is_compressed? ⇒ Boolean
- #read_file(file, to) ⇒ Object
- #read_full_size ⇒ Object
- #strip_header(buffer = nil) ⇒ Object
Instance Method Details
#compress ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/swiff/compression.rb', line 16 def compress compressor = Zlib::Deflate.new data = compressor.deflate(strip_header(decompressed_bytes), Zlib::FINISH) data = bytes[0,8] + data data[0] = ?C data end |
#decompress ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/swiff/compression.rb', line 24 def decompress decompressor = Zlib::Inflate.new swf = decompressor.inflate(strip_header) swf = bytes[0,8] + swf swf[0] = ?F swf end |
#is_compressed? ⇒ Boolean
45 46 47 |
# File 'lib/swiff/compression.rb', line 45 def is_compressed? bytes[0] == ?C end |
#read_file(file, to) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/swiff/compression.rb', line 5 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 |
#read_full_size ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/swiff/compression.rb', line 32 def read_full_size buff = File.open(@path,"rb") do |f| f.seek(4,IO::SEEK_CUR) f.read 4 end buff.unpack("L")[0] end |
#strip_header(buffer = nil) ⇒ Object
40 41 42 43 |
# File 'lib/swiff/compression.rb', line 40 def strip_header(buffer = nil) buffer ||= bytes buffer[8,bytes.size-8] end |