Created
February 2, 2018 19:21
-
-
Save righthandabacus/25056f05f2bd941f7888cda88b60a7d0 to your computer and use it in GitHub Desktop.
Extract zip file with filename encoding conversion
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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'zip' | |
require 'fileutils' | |
# parse command line arguments | |
options = {} | |
OptionParser.new do |opt| | |
opt.on('-f FROM_ENC') {|o| options[:from_enc] = o} | |
opt.on('-t TO_ENC') {|o| options[:to_enc] = o} | |
end.parse! | |
options[:zipfile] = ARGV.pop | |
raise OptionParser::MissingArgument if options[:from_enc].nil? | |
raise OptionParser::MissingArgument if options[:to_enc].nil? | |
raise OptionParser::MissingArgument if options[:zipfile].nil? | |
from_enc = Encoding.find(options[:from_enc]) | |
to_enc = Encoding.find(options[:to_enc]) | |
Zip::File.open(options[:zipfile]) do |zipfile| | |
zipfile.each do |entry| | |
pathname = entry.name | |
pathname.force_encoding(from_enc) | |
pathname = pathname.encode!(to_enc) | |
puts pathname | |
dirname = File.dirname(pathname) | |
FileUtils.mkdir_p(dirname) unless File.directory?(dirname) | |
entry.extract(pathname) | |
end | |
end | |
# vim:set ts=4 sw=4 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment