Source

main / Source / WebService / Web.Admin / Common / ZipUtility.cs

Naethra D ba1cb57 

























































































































































using System;
using System.Collections;
using System.IO;
using Ionic.Zip;

namespace DataUp.Web.Admin
{
    public class ZipUtilities
    {
        /// <summary>
        /// Creates Zip file 
        /// </summary>
        /// <param name="inputFolderPath">Folder to zip</param>
        /// 
        /// <param name="password">password</param>
        public static MemoryStream ZipFiles(string inputFolderPath, string password)
        {
            // generate file list
            ArrayList arrOfFiles = GenerateFileList(inputFolderPath);

            MemoryStream compressStream = new MemoryStream();
            using (ZipFile zip = new ZipFile())
            {
                foreach (string file in arrOfFiles)
                {
                    zip.AddFile(file, "");
                }
                zip.Save(compressStream);
            }

            return compressStream;
        }

        /// <summary>
        /// Unzip the Files to destination folder
        /// </summary>
        /// <param name="zipPathAndFile">Zip file Path </param>
        /// <param name="outputFolder">Destination folder  </param>
        /// <param name="password">Password</param>
        /// <param name="deleteZipFile">Deletes Zip file after unzip</param>
        public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
        {
            try
            {
                ClearReadOnlyAttribute(zipPathAndFile);
                using (ZipFile zip = ZipFile.Read(zipPathAndFile))
                {
                    foreach (ZipEntry entry in zip.Entries)
                    {
                        string ofileName = Path.GetFileName(entry.FileName);
                        if (ofileName != String.Empty)
                        {
                            if (entry.FileName.IndexOf(".ini") < 0)
                            {
                                string fullPath = outputFolder + Constants.PathDelimter + ofileName;
                                fullPath = fullPath.Replace("\\ ", Constants.PathDelimter);
                                string fullDirPath = Path.GetDirectoryName(fullPath);
                                using (var stream = entry.OpenReader())
                                {
                                    FileStream streamWriter = File.Create(fullPath);
                                    int size = 2048;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = stream.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    streamWriter.Close();
                                }
                            }
                        }
                    }

                }
                if (deleteZipFile)
                    File.Delete(zipPathAndFile);
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (FileNotFoundException)
            {
            }
            catch (PathTooLongException)
            {
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Generate file List
        /// </summary>
        /// <param name="inputDirectory">Directory</param>
        /// <returns></returns>
        public static ArrayList GenerateFileList(string inputDirectory)
        {
            ArrayList files = new ArrayList();
            bool Empty = true;

            // add each file in directory
            foreach (string file in Directory.GetFiles(inputDirectory))
            {
                files.Add(file);
                Empty = false;
            }

            if (Empty)
            {
                if (Directory.GetDirectories(inputDirectory).Length == 0)
                {
                    // if directory is completely empty, add it
                    files.Add(inputDirectory + @"/");
                }
            }

            // Zip All files in the sub folders.
            foreach (string dirs in Directory.GetDirectories(inputDirectory))
            {
                foreach (object obj in GenerateFileList(dirs))
                {
                    files.Add(obj);
                }
            }

            return files;
        }

        private static void ClearReadOnlyAttribute(string file)
        {
            FileAttributes fa;

            // Make sure the file exists, or else this is worthless
            if (!File.Exists(file))
            {
                return;
            }

            // Get the old file attributes.
            fa = File.GetAttributes(file);

            // Clear the read only bit.
            fa = fa & ~FileAttributes.ReadOnly;
            File.SetAttributes(file, fa);
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.