Created
June 23, 2014 23:46
-
-
Save hkondo/94c5948fcfb5035333e5 to your computer and use it in GitHub Desktop.
Extract files from multipart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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