Exemplo n.º 1
0
        /* Create the archive */
        //private void run(object sender, DoWorkEventArgs e)
        private void run(string output_filename)
        {
            try
            {
                // Get archive and compression format
                int ArchiveFormatIndex = archiveFormatList.SelectedIndex;
                int CompressionFormatIndex = compressionFormatList.SelectedIndex;

                // Get output filename
                //string output_filename = FileSelectionDialog.SaveFile("Create Archive", String.Empty, String.Format("{0} ({1})|{1}", ArchiveFilters[archiveFormatList.SelectedIndex][0], ArchiveFilters[archiveFormatList.SelectedIndex][1]));
                //if (output_filename == null || output_filename == String.Empty)
                //    return;

                // Disable the window and show the status box
                PanelContent.Enabled = false;
                //status.Visible = true;

                /* Start creating the archive */
                Archive archive = new Archive(ArchiveFormats[archiveFormatList.SelectedIndex], Path.GetFileName(output_filename));

                using (FileStream outputStream = new FileStream(output_filename, FileMode.Create, FileAccess.ReadWrite))
                {
                    // Get the block size
                    int blockSize;
                    if (blockSizes[ArchiveFormatIndex].Enabled && blockSizes[ArchiveFormatIndex].Text.Length > 0)
                        blockSize = int.Parse(blockSizes[ArchiveFormatIndex].Text);
                    else
                        blockSize = ArchiveBlockSizes[ArchiveFormatIndex][0];

                    /* Create and write the header */
                    bool[] settings = GetSettings(ArchiveFormatIndex);
                    uint[] offsetList;
                    MemoryStream header = archive.CreateHeader(fileList.ToArray(), archiveFilenames.ToArray(), blockSize, settings, out offsetList);
                    outputStream.Write(header);

                    /* Add the files */
                    for (int i = 0; i < fileList.Count; i++)
                    {
                        // Set the current file
                        status.CurrentFile = i;

                        /* Pad file so we can have the correct block offset */
                        while (outputStream.Position < offsetList[i])
                            outputStream.WriteByte(archive.PaddingByte);

                        using (Stream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read))
                        {
                            /* Format the file so we can add it */
                            Stream inputFile = inputStream;
                            inputFile = archive.FormatFileToAdd(ref inputFile);
                            if (inputFile == null)
                                throw new Exception();

                            /* Write the data to the file */
                            outputStream.Write(inputFile);
                        }
                    }

                    /* Pad file so we can have the correct block offset */
                    while (outputStream.Position % blockSize != 0)
                        outputStream.WriteByte(archive.PaddingByte);

                    /* Write the footer */
                    MemoryStream footer = archive.CreateFooter(fileList.ToArray(), archiveFilenames.ToArray(), blockSize, settings, ref header);
                    if (footer != null)
                    {
                        outputStream.Write(footer);

                        /* Pad file so we can have the correct block offset */
                        while (outputStream.Position % blockSize != 0)
                            outputStream.WriteByte(archive.PaddingByte);
                    }

                    /* Compress the data if we want to */
                    if (compressionFormatList.SelectedIndex != 0)
                    {
                        Compression compression = new Compression(outputStream, output_filename, CompressionFormats[CompressionFormatIndex - 1]);
                        MemoryStream compressedData = compression.Compress();
                        if (compressedData != null)
                        {
                            /* Clear the output stream and write the compressed data */
                            outputStream.Position = 0;
                            outputStream.SetLength(compressedData.Length);
                            outputStream.Write(compressedData);
                        }
                    }
                }

                this.Close();
            }
            catch
            {
                this.Close();
            }
            finally
            {
                status.Close();
            }
        }