Skip to content

Instantly share code, notes, and snippets.

@ne-sachirou
Created March 27, 2011 23:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ne-sachirou/889769 to your computer and use it in GitHub Desktop.
Save ne-sachirou/889769 to your computer and use it in GitHub Desktop.
Simple unzip by Ruby and by JScript.
// http://d.hatena.ne.jp/hetappi/20080819/1219157144
function unzip(zipfile, // @param String: Zipped file path
unzipdir) { // @param String: Unzip target directory path
var fso = new ActiveXObject('Scripting.FileSystemObject'),
shell = new ActiveXObject('Shell.Application'),
dst, zip;
if (!unzipdir) {
unzipdir = '.';
}
if (!fso.FolderExists(unzipdir)) {
fso.CreateFolder(unzipdir);
}
dst = shell.NameSpace(fso.getFolder(unzipdir).Path);
zip = shell.NameSpace(fso.getFile(zipfile).Path);
if (fso.FileExists(zipfile)) {
dst.CopyHere(zip.Items(), 4 + 16);
}
}
#License: Public Domain
require 'zip/zip'
# Unzip ziped file.
# unzip_file 'under/from.zip', '.'
def unzip_file zip_filename, target_foldername
Dir.mkdir target_foldername unless File.exist? target_foldername
Zip::ZipFile.foreach zip_filename do |zipentry|
puts target = "#{target_foldername}/#{zipentry.name}"
if zipentry.file?
destdirs = File.dirname(target).split('/')
destdirs.each_index {|i| Dir.mkdir destdirs[0..i].join('/') unless File.exist? destdirs[0..i].join('/')}
File.delete target if File.exist? target
zipentry.extract target
end
end
end
@ne-sachirou
Copy link
Author

https://gist.github.com/889769/d91d20e49b32363d7331eb25a0b05ca7b90ee3da
Bug fix for when zip file contain imaginally top directory.

@ne-sachirou
Copy link
Author

@ne-sachirou
Copy link
Author

@ne-sachirou
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment