Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

ZipBuilder - A zip package creation utility for collections of large files

, 28 Sep 2009 BSD
SharpZipLib is a pretty great Open Source compression library for .NET. Out of the box, however, it does have a few problems with archives for files that are larger than 2GB. ZipBuilder was created to provide a convenient way to let people programmatically create archive packages with large files.
zipbuilder_demo.zip
ZipBuilder_demo
ICSharpCode.SharpZipLib.dll
Zipper.exe
zipbuilder_src.zip
ICSharpCode.SharpZipLib.dll
#region LICENSE
/*
 * ZipBuilder - A zip package creation utility for collections of large files.
 *
 * Copyright (c) 2009, Christopher Hahn <chahn.chris@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of the <organization> nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#endregion
using System;
using System.IO;

namespace ZipTools {
    public class Zipper {
        public static void Main(string[] argList) {
            string outputFile = String.Empty;
            string fileSpec = String.Empty;
            bool showHelp = false;

            var parser = new CommandParser("A tool for creating zip packages");

            parser.Argument("?", "help", 
                            "Shows this help screen",
                            (p, v) => { showHelp = true; });

            parser.Argument("o", "output",
                            "Specifies what output file to use",
                            "ouptut_file",
                            CommandArgumentFlags.TakesParameter | 
                            CommandArgumentFlags.Required,
                            (p, v) => { outputFile = v; });
            
            parser.Argument("f", "files",
                            "A file specification of which files to package",
                            "files",
                            CommandArgumentFlags.TakesParameter |
                            CommandArgumentFlags.Required,
                            (p, v) => { fileSpec = v; });

            parser.Parse();

            if (!showHelp) {
                if (!String.IsNullOrEmpty(outputFile)) {
                    //
                    // tell the user what file we're creating
                    //
                    Console.WriteLine("Creating output package " + Path.GetFullPath(outputFile));

                    //
                    // create the package object and hookup it's progress
                    // events
                    //
                    var packer = new ZipBuilder();

                    //
                    // set block size to 1MB for demonstration purposes
                    //
                    packer.BlockSize = 1024 * 1024; 

                    packer.FileStarted = (string f) => {
                        Console.WriteLine(String.Empty);
                        Console.WriteLine("Adding " + f);
                    };

                    packer.CrcProgress = (long p, long t) => {
                        float percentComplete = 
                        (float)p / (float)t * 100;

                        var msg = String.Format("CRC (bytes): {0:n0}/{1:n0} ({2:0}%)",
                                                (p),
                                                (t),
                                                percentComplete);
                    
                        Console.Write(String.Empty.PadLeft(70, ' ') + "\r");
                        Console.Write(msg + "\r");
                        
                        if (p == t) {
                            Console.WriteLine();
                        }
                    };

                    packer.FileProgress = (long p, long t) => {
                        float percentComplete = 
                        (float)p / (float)t * 100;

                        var msg = String.Format("FILE (bytes): {0:n0}/{1:n0} ({2:0}%)",
                                                (p),
                                                (t),
                                                percentComplete);
                    
                        Console.Write(String.Empty.PadLeft(70, ' ') + "\r");
                        Console.Write(msg + "\r");
                        
                        if (p == t) {
                            Console.WriteLine();
                        }
                    };
                    
                    packer.CreatePackage(outputFile, fileSpec);
                } else {
                    Console.WriteLine("ERROR: You must specify an output file and a set of files to package.");
                    Console.WriteLine(parser.GetHelp());
                }
            } else {
                Console.WriteLine(parser.GetHelp());
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Christopher Hahn
Chief Technology Officer Appature, Inc.
United States United States
I have been developing software professionally for over 12 years. I have been a developer at ATI Research, Microsoft, a Social Bookmarking website called Faves.com, and most recently I have started a company called Appature, Inc focusing on Enterprise Marketing Management software in the Healthcare space.
 
If you find any of my submissions here useful, or even if you don't, I'd love to hear from you!

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 28 Sep 2009
Article Copyright 2009 by Christopher Hahn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid