Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / Windows Forms

Deployer

Rate me:
Please Sign up or sign in to vote.
4.43/5 (17 votes)
30 Mar 2012Apache14 min read 82K   543   91  
Automate deployment of Windows Services, ClickOnce, and other .NET applications.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace UsefulHeap.Zip
{
    class ZipDirectory
    {
        public static void CompressDirectory(string toZipFile, string directoryPath)
        {
            ZipArchive newArchive = new ZipArchive(toZipFile, FileAccess.Write);

            CompressDirectory(newArchive, directoryPath);

            newArchive.Close();
        }


        public static void CompressDirectory(ZipArchive archive, string directoryPath)
        {
            CompressDirectory(archive, directoryPath, "");
        }

        private static void CompressDirectory(ZipArchive archive, string directoryPath, string relativePrefix)
        {
            DirectoryInfo di = new DirectoryInfo(directoryPath);

            foreach (DirectoryInfo childDir in di.GetDirectories())
            {
                string newRelativePrefix = relativePrefix + childDir.Name + "/";
                //                archive[newRelativePrefix].Write(new byte[] { }, 0, 0);
                CompressDirectory(archive, childDir.FullName, newRelativePrefix);
            }

            foreach (FileInfo fi in di.GetFiles())
            {
                byte[] content = File.ReadAllBytes(fi.FullName);
                archive[relativePrefix + fi.Name].Write(content, 0, content.Length);
            }
        }

        public static void DecompressAtDirectory(string zipFile, string directoryPath)
        {
            ZipArchive newArchive = new ZipArchive(zipFile, FileAccess.Read);

            DecompressAtDirectory(newArchive, directoryPath);

            newArchive.Close();
        }

        public static void DecompressAtDirectory(ZipArchive archive, string directoryPath)
        {
            DirectoryInfo di = new DirectoryInfo(directoryPath);
            if (!di.Exists)
                di.Create();

            // Create directories
            foreach (ZipEntry ze in archive.GetAllEntries())
            {
                if (ze.Name.Contains("/"))
                    di.CreateSubdirectory(ze.Name.Substring(0, ze.Name.LastIndexOf('/')));
            }

            foreach (ZipEntry ze in archive.GetAllEntries())
            {
                if (!ze.IsDirectory)
                {
                    using (FileStream fs = new FileStream(di.FullName + "\\" + ze.Name, FileMode.OpenOrCreate))
                    {
                        byte[] file = new byte[ze.Length];
                        archive[ze.Name].Read(file, 0, file.Length);

                        fs.Write(file, 0, file.Length);
                    }
                }
            }
        }
    }
}

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 Apache License, Version 2.0


Written By
Chief Technology Officer
United States United States
If you liked this article, consider reading other articles by me. For republishing article on other websites, please contact me by leaving a comment.

Comments and Discussions