Ruby uses the zip gem to write a simple compression and decompression gadget

Source: Internet
Author: User

How can we be reduced to writing compression and decompression tools with Ruby under UNIX? Go straight to the shell! But please allow this cat to be ashamed to play with Ruby this time! In fact, Ruby Gem has a lot of compression decompression package, I choose Zip, perhaps because the name conforms to the KISS principle! However, some of the classes found in the zip in writing do not have the instance methods described in the documentation, perhaps not implemented on a platform yet??

First of all, this tool if you unzip the file with the name of the case will be directly overwrite the original file without any hint! It is important to note that if some files are lost, don't blame the cat!

The code also takes into account the situation of multiple files, if it is compressed multiple files will be compressed by default each file, such as: Zip.rb a B c D will produce a.zip. D.zip four compressed files; But I also consider the reality of the situation, wrote a separate Zip_n2one method to compress multiple files into a file, this can see the code implementation, very clear, if the decompression of multiple files will be extracted in turn each file, if the file has the same name will be described as a direct overwrite.

The code does not take into account that if multiple compressed files are basename the same situation, that is, zip.rb A.dat: /a.dat ~/a.dat the situation. If so, I estimate that only one entry file in the compressed package is the last ~/a.dat, to avoid this situation need to do extra judgment, I am not writing a real production tool, just a toy, so donuts.

A problem was found in the test code: How to implement deleting all the files in a directory, except for the zip file. This is directly with the shell:

[Email protected]: tmp$ls|grep-v. *.ZIP|XARGS-N1 RM


What if I delete all the zip files? Can do this:

[Email protected]kissair:tmp$ls|grep. *.ZIP|XARGS-N1 RM

but silly, why not direct RM *.zip? The code below, write faster, so some implementations slightly "clumsy", the optimization of the not optimized, the refactoring is not refactoring! Finally remind: If you want multiple files to be packaged in a zip, use the Zip_n2one method, note that the final zip file name of the Zip_n2one method is hard-coded, which is somewhat unpleasant, but this is only a test, so you can always rewrite the children's shoes:
#!/usr/bin/ruby# Simple Compression Decompression tool #code by Patty |hopy 2014-12-01require ' Zip/zip ' def sh_e (e) e.backtrace.each {|s|puts s}puts "ERR: #{e.to_s} \ n "end# get all entry names in the Zip file def get_entries_name (path) Full_path = File.expand_path (path) entries = []zip:: Zipinputstream::open (Full_path) do |io|while (entry = io.get_next_entry) entries << Entry.nameendendentriesenddef Unzip (path) Full_path = File.expand_path (path) entries = get_entries_name (path) Zip:: Zipfile.open (Full_path) do |f|entries.each do |entry|f.extract (entry,entry) {true}puts "unzip #{entry} succeed!" Endendrescue =>esh_e (E) Exit 3enddef Zip_n2one (paths,zip_path) Full_zip_path = File.expand_path (zip_path) f = Zip:: Zipfile.open (full_zip_path,zip::zipfile::create) paths.each do |path|full_path = File.expand_path (path) F.add ( File.basename (path), Full_path) {true}puts ' Add #{path} to #{full_zip_path} ' endf.closeputs ' All files are zip to #{full_zip _path} "rescue =>esh_e (E) Exit 4enddef Zip (path) Full_path = File.expand_path (path) Dir_name = File.dirname (full_path) Only_name = file.basename (Path, ". *") Only_zip_name = Only_name + ". zip" Full_zip_path = dir_name + '/' + only_zip_name f = Zip::zipfile.open (full_zip_path,zip::zipfile::create) f.add (file.basename (path), Full_path) {true}f.closeputs] Create #{full_zip_path} succeed! " Rescue =>esh_e (e) Exit 5endis_unzip = falsecase argv.countwhen 0puts "Usage #{$0} [-u] files [...]" Exit 1when 1if argv[0] = = "-U" puts "Err:unzip without filename!" Exit 2endelseif argv[0] = = "-U" is_unzip = true# Remove the option-U from the parameter list argv.shiftendendif Is_unzipargv.each {|file_path|unzip ( File_path)}else#argv.each {|file_path|zip (file_path)}zip_n2one (ARGV, "Total.zip") end

Ruby uses the zip gem to write a simple compression and decompression gadget

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.