Created
May 2, 2012 01:09
-
-
Save sptramer/2572795 to your computer and use it in GitHub Desktop.
CLI zip support
This file contains 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
Hey there! |
This file contains 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
// npm packages for zip: | |
// zip: 202 days | |
// adm-zip: 41 days | |
// zip-utils: 4 days (0.0.1) [repo no longer exists] | |
// zipper: 96 days | |
// zipper-insane: 136 days | |
// node-native-zip: 2w (1.1.0) | |
// node-zip: 2 days (0.0.1) | |
// zippy: 223 days | |
// CANDATES ARE: node-native-zip, node-zip | |
/* | |
* node-native-zip: https://github.com/janjongboom/node-native-zip | |
* - Only supports write, no read (acceptable) | |
* - Only supports STORE, no compression | |
* (unacceptable; some Android APK files require DEFLATE) | |
* | |
* REJECTED | |
*/ | |
/* | |
* node-zip: https://github.com/daraosn/node-zip (MIT license) | |
* requires: node.js fs (Stable: 3) mv (Stable: 3) | |
* - Depends upon JSZip (http://stuartk.com/jszip/) | |
* - JSZip is: 5m old, GPLv3 and/or MIT (???), supports zip+unzip | |
* - Unlikely to ever be updated | |
* - Incomplete documentation (for JSZip) | |
* - HAS CRITICAL IMPLEMENTATION BUG: | |
* generate()d zipfiles lack end-of-dir signature | |
* | |
* REJECTED - THIS SAMPLE CODE DEMONSTRATES IMPLEMENTATION BUG. | |
* Just try and unzip the resulting zipfile. | |
*/ | |
// Make sure you npm install node-zip first | |
var zip = new require('node-zip')(); | |
var fs = require('fs'); | |
var path = require('path'); | |
var console = require('console'); | |
function crawl(dir, zipfile) { | |
if (!fs.statSync(dir).isDirectory()) { | |
return; | |
} | |
var files = fs.readdirSync(dir); | |
console.log(files); | |
for (var i in files) { | |
var file = files[i]; | |
var stat = fs.statSync(path.join(dir, file)); | |
if (stat.isDirectory()) { | |
console.log('dir: '+file); | |
crawl(path.join(dir, file), zip.folder(file)); | |
} | |
else if (stat.isFile()) { | |
console.log('file: '+file); | |
zip.file(file, fs.readFileSync(path.join(dir, file), 'utf8')); | |
} | |
} | |
} | |
crawl(__dirname, zip); | |
fs.writeFileSync('package.zip', zip.generate({ compression:"DEFLATE" })); |
This file contains 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 python | |
# distributed w/python 2.7 & 3.2, compatible with both | |
import zipfile | |
import os | |
import os.path | |
if __name__ == "__main__": | |
#load files into a zipfile from current directory | |
#used for packaging APK and IPA | |
package = zipfile.ZipFile('package.zip', 'w') | |
for dirpath, dirs, files in os.walk('.', topdown=False): | |
for dir in dirs: | |
print 'dir: ', dir | |
package.write(os.path.join(dirpath, dir)) | |
for filename in files: | |
print 'file: ', filename | |
if filename == "package.zip": | |
continue | |
package.write(os.path.join(dirpath, filename)) | |
package.close() | |
# Read the zipfile | |
package = zipfile.ZipFile('package.zip', 'r') | |
hello_zip = package.open("hello.txt") | |
hello_txt = open("hello.txt") | |
while True: | |
zip_c = hello_zip.read(1) | |
hello_c = hello_txt.read(1) | |
if zip_c != hello_c: | |
print "BAD ZIPFILE" | |
break | |
if zip_c == "": | |
print "Correctly zipped!" | |
break | |
hello_zip.close() | |
hello_txt.close() | |
package.close() |
This file contains 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 | |
# Both provide equivalent zip/zip namespaces: | |
# gem rubyzip - 1.8.x only | |
# gem zip - 1.9.x only | |
require 'zip/zip' | |
require 'pathname' | |
# Write test | |
Zip::ZipFile.open('package.zip', Zip::ZipFile::CREATE) do |zipfile| | |
root = Pathname.new(Dir.pwd) | |
root.find() do |file| | |
next if file.to_s() == Dir.pwd | |
zipfile.add(file.relative_path_from(root), file.to_path()) if file.file? | |
zipfile.mkdir(file.relative_path_from(root)) if file.directory? | |
end | |
end | |
# Read test | |
Zip::ZipFile.open('package.zip') do |zipfile| | |
zip_stream = zipfile.get_input_stream("hello.txt") | |
file_stream = open("hello.txt") | |
# Make sure the files are equal! | |
loop do | |
e1 = zip_stream.next rescue :eof | |
e2 = file_stream.next rescue :eof | |
if e1 != e2 | |
puts "BAD ZIPFILE" | |
end | |
if e1 == :eof | |
puts "Correctly zipped!" | |
break | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment