Skip to content

Instantly share code, notes, and snippets.

@hkondo
Created June 23, 2014 23:46
Show Gist options
  • Select an option

  • Save hkondo/94c5948fcfb5035333e5 to your computer and use it in GitHub Desktop.

Select an option

Save hkondo/94c5948fcfb5035333e5 to your computer and use it in GitHub Desktop.
Extract files from multipart.
boundary='--myboundary'
ct = 'Content-type:'
cl = 'Content-length:'
no = 1
File.open(ARGV[0], 'r'){|f|
while(true)
length = 0
# skip to boundary.
while(ll = f.readline("\r\n"))
break if ll.chomp == boundary
end
while(ll = f.readline("\r\n"))
if ll.start_with?(cl)
length = ll.split(':').last.to_i
break
end
end
# skip empty line.
while(ll = f.readline("\r\n"))
break if ll.chomp == ''
end
name = sprintf("%08d", no) + ".jpg"
puts "Saving #{name} with #{length} bytes."
bytes = f.read(length)
File.open(name, 'w'){|s|
s.print(bytes)
}
no = no + 1
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment