AddDirectory() public method

Add a directory entry to the archive.
public AddDirectory ( string directoryName ) : void
directoryName string The directory to add.
return void
Example #1
3
 /// <summary>
 /// Iterate thru all the filesysteminfo objects and add it to our zip file
 /// </summary>
 /// <param name="fileSystemInfosToZip">a collection of files/directores</param>
 /// <param name="z">our existing ZipFile object</param>
 private static void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
 {
     //check whether the objects are null
     if (fileSystemInfosToZip != null && z != null)
     {
         //iterate thru all the filesystem info objects
         foreach (FileSystemInfo fi in fileSystemInfosToZip)
         {
             //check if it is a directory
             if (fi is DirectoryInfo)
             {
                 DirectoryInfo di = (DirectoryInfo)fi;
                 //add the directory
                 z.AddDirectory(di.FullName);
                 //drill thru the directory to get all
                 //the files and folders inside it.
                 GetFilesToZip(di.GetFileSystemInfos(), z);
             }
             else
             {
                 //add it
                 z.Add(fi.FullName);
             }
         }
     }
 }
        public void CompressDataInToFile(string directory, string password, string outputFile)
        {
            var fullFileListing = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories);
            var directories = Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories);

            _logger.Information("Creating ZIP File");
            using (var zip = new ZipFile(outputFile))
            {
                zip.UseZip64 = UseZip64.On;

                _logger.Information("Adding directories..");
                foreach (var childDirectory in directories)
                {
                    _logger.Information(string.Format("Adding {0}", childDirectory.Replace(directory, string.Empty)));
                    zip.BeginUpdate();
                    zip.AddDirectory(childDirectory.Replace(directory, string.Empty));
                    zip.CommitUpdate();
                }

                _logger.Information("Adding files..");
                foreach (var file in fullFileListing)
                {
                    _logger.Information(string.Format("Adding {0}", file.Replace(directory, string.Empty)));
                    zip.BeginUpdate();
                    zip.Add(file, file.Replace(directory, string.Empty));
                    zip.CommitUpdate();
                }

                _logger.Information("Setting password..");
                zip.BeginUpdate();
                zip.Password = password;
                zip.CommitUpdate();
            }
        }
Example #3
0
        void SetContent(string loc, byte[] content)
        {
            ZipFile zipFile = new ZipFile(FileLoc);

            // Must call BeginUpdate to start, and CommitUpdate at the end.
            zipFile.BeginUpdate();
            if (loc.Contains('/'))
            {
                int i = loc.IndexOf('/');
                string dir = loc.Remove(i + 1, loc.Length - i - 1);
                if (zipFile.FindEntry(dir, true) < 0)
                {
                    zipFile.AddDirectory(dir);
                }
            }
            CustomStaticDataSource sds = new CustomStaticDataSource();
            using (MemoryStream ms = new MemoryStream(content))
            {
                sds.SetStream(ms);

                // If an entry of the same name already exists, it will be overwritten; otherwise added.
                zipFile.Add(sds, loc);

                // Both CommitUpdate and Close must be called.
                zipFile.CommitUpdate();
                zipFile.Close();
            }
        }
Example #4
0
        void AddDirectory(string loc)
        {
            ZipFile zipFile = new ZipFile(FileLoc);

            // Must call BeginUpdate to start, and CommitUpdate at the end.
            zipFile.BeginUpdate();
            zipFile.AddDirectory(loc);
            // Both CommitUpdate and Close must be called.
            zipFile.CommitUpdate();
            zipFile.Close();
        }
Example #5
0
        void AddDirectory(string loc)
        {
            ZipFile zipFile = new ZipFile(this.zipArchive);

            // Must call BeginUpdate to start, and CommitUpdate at the end.
            zipFile.BeginUpdate();
            zipFile.AddDirectory(loc);
            // Both CommitUpdate and Close must be called.
            zipFile.CommitUpdate();
            zipFile.IsStreamOwner = false; zipFile.Close();

            this.zipArchive.Position = 0;
        }
        /// <summary>
        /// Recursively adds folders and files to archive
        /// </summary>
        /// <param name="root">The root directory.</param>
        /// <param name="zipArchive"></param>
        /// <param name="sourceDirectory"></param>
        /// <param name="recurse"></param>
        private static void AddDirectoryFilesToZip(string root,ZipFile zipArchive, string sourceDirectory, bool recurse)
        {
            // Add this directory
            zipArchive.AddDirectory(root + "/" + sourceDirectory);

            // Recursively add sub-folders
            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToZip(root,zipArchive, directory, recurse);
                }
            }

            // Add files
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                string name = filename;

                if (name.StartsWith(@".\"))
                {
                    name = name.Substring(2, name.Length - 2);
                }

                if (!String.IsNullOrEmpty(root))
                {
                    name = root + @"\" + name;
                }

                Console.WriteLine("{0} -> {1}", filename, name);

                zipArchive.Add(filename, name);

            }
        }
Example #7
0
        private void Flush(bool refresh)
        {
            lock (this)
            {
                var filesChanged = zipFileInfos.Values.Where(c => c.ShadowFile != null).ToList();
                var filesDeleted = zipFileInfos.Values.Where(c => !c.Exists && c.ZipEntry != null).ToList();

                var setOfPreviousDirectories = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                foreach (ZLib.ZipEntry zipEntry in zipFile)
                {
                    if (zipEntry.IsDirectory)
                    {
                        setOfPreviousDirectories.Add("/" + zipEntry.Name.Substring(0, zipEntry.Name.Length - 1));
                    }
                    else
                    {
                        var x = zipEntry.Name.LastIndexOf('/');

                        if (x > 0)
                        {
                            var path = zipEntry.Name.Substring(0, x);

                            setOfPreviousDirectories.Add("/" + path);
                        }
                    }
                }

                var setOfCurrentImplicitDirectories = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);
                var setOfCurrentDirectories         = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                foreach (var zipFileInfo in zipFileInfos.Values)
                {
                    if (zipFileInfo.Exists)
                    {
                        var x = zipFileInfo.AbsolutePath.LastIndexOf('/');

                        if (x > 0)
                        {
                            var path = zipFileInfo.AbsolutePath.Substring(0, x);

                            setOfCurrentDirectories.Add(path);
                            setOfCurrentImplicitDirectories.Add(path);
                        }
                    }
                }

                foreach (var zipDirectoryInfo in zipDirectoryInfos.Values.Where(c => c.Exists))
                {
                    setOfCurrentDirectories.Add(zipDirectoryInfo.AbsolutePath);
                }

                var setOfNewDirectories      = new HashSet <string>(setOfCurrentDirectories.Where(c => !setOfPreviousDirectories.Contains(c)), StringComparer.InvariantCultureIgnoreCase);
                var setOfDeletedDirectories  = new HashSet <string>(setOfPreviousDirectories.Where(c => !setOfCurrentDirectories.Contains(c)), StringComparer.InvariantCultureIgnoreCase);
                var setOfDirectoriesToCreate = new HashSet <string>(setOfNewDirectories.Where(c => !setOfCurrentImplicitDirectories.Contains(c)), StringComparer.InvariantCultureIgnoreCase);

                setOfDirectoriesToCreate.Remove("/");

                if (filesChanged.Count > 0 || filesDeleted.Count > 0)
                {
                    zipFile.BeginUpdate();

                    try
                    {
                        foreach (var zipFileInfo in filesChanged)
                        {
                            var shadowFile = zipFileInfo.ShadowFile;

                            var name = zipFileInfo.AbsolutePath;

                            try
                            {
                                zipFile.Add(new StreamDataSource(shadowFile.GetContent().GetInputStream()), name);
                            }
                            catch (FileNodeNotFoundException)
                            {
                            }
                        }

                        foreach (var zipFileInfo in filesDeleted)
                        {
                            zipFile.Delete(zipFileInfo.ZipEntry);
                        }

                        foreach (var directoryToCreate in setOfDirectoriesToCreate)
                        {
                            zipFile.AddDirectory(directoryToCreate);
                        }

                        foreach (var directory in setOfDeletedDirectories)
                        {
                            // SharpZipLib currently doesn't support removing explicit directories
                        }
                    }
                    finally
                    {
                        zipFile.CommitUpdate();
                    }
                }

                if (refresh)
                {
                    this.RefreshNodeInfos();
                }
            }
        }
        /// <summary>
        /// Generates the image.
        /// </summary>
        /// <remarks>CFI, 2012-02-24</remarks>
        private void GenerateImage()
        {
            #region Check parameter:

            if (!Directory.Exists(textBoxInputFilePath.Text))
                throw new DirectoryNotFoundException("The input directory was not found!");
            if (!Directory.Exists(Path.GetDirectoryName(textBoxOutputFile.Text)))
                throw new DirectoryNotFoundException("The output path is invalid!");
            if (textBoxStickName.Text.Length > 11 || textBoxStickName.Text == string.Empty)
                throw new ArgumentException("Invalid Stick name!");
            if (textBoxStickID.Text.Length != 9 || textBoxStickID.Text[4] != '-')
                throw new ArgumentException("Invalid Stick ID!");

            #endregion

            string outputFilePath = Path.ChangeExtension(textBoxOutputFile.Text, ".MLifterStick");

            #region Create Zip:

            Stream output = File.Create(outputFilePath);
            ZipOutputStream zipStream = new ZipOutputStream(output);
            ZipFile zip = new ZipFile(output);

            #endregion

            #region Fill Zip:

            zip.BeginUpdate();

            int pos = 1;
            string[] files = Directory.GetFiles(textBoxInputFilePath.Text, "*.*", SearchOption.AllDirectories);
            List<string> folders = new List<string>();
            foreach (string file in files)
            {
                toolStripStatusLabelMessage.Text = string.Format("Adding file {0} of {1}...", pos, files.Length);
                toolStripProgressBarStatus.Value = Convert.ToInt32(pos++ * 1.0 / files.Length * 100);
                Application.DoEvents();

                string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
                string zipFile = Path.Combine(dir, Path.GetFileName(file));

                if (dir.Length > 0)
                {
                    if (!folders.Contains(dir))
                    {
                        zip.AddDirectory(dir);
                        folders.Add(dir);
                    }
                }
                zip.Add(new FileDataSource(file), zipFile);
            }

            toolStripStatusLabelMessage.Text = "Saving image. THIS COULD RUN VERY LONG - PLEASE WAIT!";
            Application.DoEvents();

            zip.CommitUpdate();

            #endregion

            VolumeDataStorage vds = new VolumeDataStorage();

            #region setting file attributes:

            foreach (string file in files)
            {
                string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
                if (dir.StartsWith("\\"))
                    dir = dir.Remove(0, 1);
                string zipFile = Path.Combine(dir, Path.GetFileName(file)).Replace(@"\", "/");
                ZipEntry entry = zip.GetEntry(zipFile);
                if (entry == null)
                    continue;
                FileAttributes attr = (new FileInfo(file)).Attributes;
                if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal))
                    vds.FileAttributeList.Add(entry.Name, attr);
                toolStripStatusLabelMessage.Text = "Setting file FileAttributes to: " + zipFile;
            }
            foreach (string folder in folders)
            {
                FileAttributes attr = (new DirectoryInfo(Path.Combine(textBoxInputFilePath.Text, folder))).Attributes;
                if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal || attr == FileAttributes.Directory))
                    vds.FolderAttributeList.Add(folder, attr);
                toolStripStatusLabelMessage.Text = "Setting folder FileAttributes to: " + folder;
            }

            #endregion

            #region Set Comment:

            vds.VolumeLabel = textBoxStickName.Text;
            vds.VolumeSerial = textBoxStickID.Text;
            string comment = VolumeDataStorage.SerializeData(vds);

            File.WriteAllText(Path.Combine(Application.StartupPath, "Comment.txt"), comment);

            zip.BeginUpdate();
            zip.SetComment(comment);
            zip.CommitUpdate();

            #endregion

            zip.Close();

            CreatedImage = outputFilePath;
        }