Ruby: shared binary data

I want to split datainto pieces let say 8154 byte:

data = Zlib::Deflate.deflate(some_very_long_string)

What would be the best way to do this?

I tried using this:

chunks = data.scan /.{1,8154}/

... but the data was lost! datahad sizefrom 11682, but when passing through each piece and adding up, sizeI ended up with a total size of 11677. 5 bytes were lost! Why?

+3
source share
2 answers

Regular expressions are not a good way to parse binary data. Use bytesand each_sliceto manage bytes. And use pack 'C*'to convert them back to strings for output or debugging:

irb> data = File.open("sample.gif", "rb", &:read)
=> "GIF89a\r\x00\r........."

irb> data.bytes.each_slice(10){ |slice| p slice, slice.pack("C*") }
[71, 73, 70, 56, 57, 97, 13, 0, 13, 0]
"GIF89a\r\x00\r\x00"
[247, 0, 0, 0, 0, 0, 0, 0, 51, 0]
"\xF7\x00\x00\x00\x00\x00\x00\x003\x00"
...........
+5
source

, .

(500x 1MB 10kB !):

def get_binary_chunks(string, size)
  Array.new(((string.length + size - 1) / size)) { |i| string.byteslice(i * size, size) }
end

:

chunks = get_binary_chunks(data, 8154)
0

Source: https://habr.com/ru/post/1665546/


All Articles