Example #1
0
        /// <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;
        }