Пример #1
0
        private ArchiveFileList GetFileList(ref Stream data, string filename, out string type)
        {
            type = null;

            try
            {
                /* Check to see if the archive is compressed */
                Compression compression = new Compression(data, filename);
                if (compression.Format != CompressionFormat.NULL)
                {
                    /* Decompress */
                    MemoryStream decompressedData = compression.Decompress();
                    if (decompressedData != null)
                        data = decompressedData;
                }

                /* Check to see if this is an archive */
                Archive archive = new Archive(data, filename);
                if (archive.Format == ArchiveFormat.NULL)
                    throw new ArchiveFormatNotSupported();

                /* Check to see if the data was translated */
                if (data != archive.GetData())
                    data = archive.GetData();

                /* Get the file list and archive type */
                type = archive.Name;
                return archive.GetFileList();
            }
            catch
            {
                return null;
            }
        }
Пример #2
0
        // Extract the files
        private void run(object sender, DoWorkEventArgs e)
        {
            // Create the file list
            List<string> fileList = new List<string>();
            foreach (string i in files)
                fileList.Add(i);

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

                // Set up the image file list for conversion
                List<string> imageFileList = new List<string>();

                try
                {
                    // Set up the file paths and open up the file
                    string InFile       = Path.GetFileName(fileList[i]);
                    string InDirectory  = Path.GetDirectoryName(fileList[i]);
                    string OutFile      = InFile;
                    string OutDirectory = InDirectory;

                    using (FileStream InputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read))
                    {
                        Stream InputData = InputStream;

                        // Decompress the file
                        if (decompressSourceFile.Checked)
                        {
                            Compression compression = new Compression(InputData, InFile);
                            if (compression.Format != CompressionFormat.NULL)
                            {
                                MemoryStream DecompressedData = compression.Decompress();
                                if (DecompressedData != null)
                                {
                                    InputData = DecompressedData;
                                    if (useStoredFilename.Checked)
                                        InFile = compression.DecompressFilename;
                                }
                            }
                        }

                        // Open up the archive
                        Archive archive = new Archive(InputData, InFile);
                        if (archive.Format == ArchiveFormat.NULL)
                            continue;
                        ArchiveFileList ArchiveFileList = archive.GetFileList();
                        if (ArchiveFileList == null || ArchiveFileList.Entries.Length == 0)
                            continue;
                        status.TotalFilesLocal = ArchiveFileList.Entries.Length;

                        // Create the directory where we will extract the files to
                        if (extractDirSameFilename.Checked && deleteSourceArchive.Checked)
                            OutDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); // Create a temporary place to store the files for now
                        else if (extractSameDir.Checked)
                            OutDirectory = InDirectory;
                        else
                            OutDirectory = InDirectory + Path.DirectorySeparatorChar + archive.OutputDirectory + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(InFile);
                        if (!Directory.Exists(OutDirectory))
                            Directory.CreateDirectory(OutDirectory);

                        // Extract the data from the archive
                        for (int j = 0; j < ArchiveFileList.Entries.Length; j++)
                        {
                            // Set the current file and get the data
                            status.CurrentFileLocal = j;
                            MemoryStream OutData = archive.GetData().Copy(ArchiveFileList.Entries[j].Offset, ArchiveFileList.Entries[j].Length);

                            // Get the filename that the file will be extracted to
                            if (extractFilenames.Checked && ArchiveFileList.Entries[j].Filename != String.Empty)
                                OutFile = ArchiveFileList.Entries[j].Filename;
                            else
                                OutFile = j.ToString().PadLeft(ArchiveFileList.Entries.Length.Digits(), '0') + FileTypeInfo.GetFileExtension(OutData);

                            // Decompress the data
                            if (decompressExtractedFile.Checked)
                            {
                                Compression compression = new Compression(OutData, OutFile);
                                if (compression.Format != CompressionFormat.NULL)
                                {
                                    MemoryStream DecompressedData = compression.Decompress();

                                    if (decompressExtractedDir.Checked)
                                    {
                                        // Write this to a different directory
                                        string TempOutFile = OutFile; // We will change it temporarily
                                        if (useStoredFilename.Checked)
                                            OutFile = compression.DecompressFilename;

                                        if (!Directory.Exists(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory))
                                            Directory.CreateDirectory(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory);
                                        using (FileStream OutputStream = new FileStream(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory + Path.DirectorySeparatorChar + OutFile, FileMode.Create, FileAccess.Write))
                                            OutputStream.Write(DecompressedData);

                                        OutFile = TempOutFile; // Reset the output file now
                                    }
                                    else
                                        OutData = DecompressedData;
                                }
                            }

                            // See if we want to extract subarchives
                            if (extractExtracted.Checked)
                            {
                                Archive archiveTemp = new Archive(OutData, OutFile);
                                if (archiveTemp.Format != ArchiveFormat.NULL)
                                {
                                    string AddFile = String.Empty;
                                    if (extractDirSameFilename.Checked && deleteSourceArchive.Checked)
                                        AddFile = fileList[i] + Path.DirectorySeparatorChar + OutFile;
                                    else
                                        AddFile = OutDirectory + Path.DirectorySeparatorChar + OutFile;

                                    fileList.Add(AddFile);
                                    status.AddFile(AddFile);
                                }
                            }

                            // Output an image
                            if (unpackImage.Checked)
                            {
                                Images images = new Images(OutData, OutFile);
                                if (images.Format != GraphicFormat.NULL)
                                    imageFileList.Add(OutDirectory + Path.DirectorySeparatorChar + OutFile); // Add this to the image conversion list (in case we need a palette file)
                            }

                            // Write the file
                            using (FileStream OutputStream = new FileStream(OutDirectory + Path.DirectorySeparatorChar + OutFile, FileMode.Create, FileAccess.Write))
                                OutputStream.Write(OutData);
                        }
                    }

                    // Process image conversion now
                    if (unpackImage.Checked && imageFileList.Count > 0)
                    {
                        // Reset the local file count
                        status.CurrentFileLocal = 0;
                        status.TotalFilesLocal = imageFileList.Count;

                        for (int j = 0; j < imageFileList.Count; j++)
                        {
                            status.CurrentFileLocal = j;

                            string InFileBitmap       = Path.GetFileName(imageFileList[j]);
                            string InDirectoryBitmap  = Path.GetDirectoryName(imageFileList[j]);
                            string OutFileBitmap      = Path.GetFileNameWithoutExtension(InFileBitmap) + ".png";
                            string OutDirectoryBitmap = InDirectoryBitmap;

                            // Load the file
                            using (FileStream InputStream = new FileStream(imageFileList[j], FileMode.Open, FileAccess.Read))
                            {
                                Images images = new Images(InputStream, Path.GetFileName(imageFileList[j]));
                                if (images.Format != GraphicFormat.NULL)
                                {
                                    if (!convertSameDir.Checked)
                                        OutDirectoryBitmap = InDirectoryBitmap + Path.DirectorySeparatorChar + images.OutputDirectory;

                                    // Start the conversion
                                    Bitmap ImageData = null;
                                    try
                                    {
                                        ImageData = images.Unpack();
                                    }
                                    catch (GraphicFormatNeedsPalette)
                                    {
                                        // We need a palette file
                                        if (File.Exists(InDirectoryBitmap + Path.DirectorySeparatorChar + images.PaletteFilename))
                                        {
                                            using (FileStream InStreamPalette = new FileStream(InDirectoryBitmap + Path.DirectorySeparatorChar + images.PaletteFilename, FileMode.Open, FileAccess.Read))
                                            {
                                                images.Decoder.PaletteData = InStreamPalette;
                                                ImageData = images.Unpack();
                                            }
                                        }
                                    }
                                    catch
                                    {
                                        continue;
                                    }

                                    // Output the image
                                    if (ImageData != null)
                                    {
                                        if (!Directory.Exists(OutDirectoryBitmap))
                                            Directory.CreateDirectory(OutDirectoryBitmap);

                                        ImageData.Save(OutDirectoryBitmap + Path.DirectorySeparatorChar + OutFileBitmap, ImageFormat.Png);
                                    }
                                }
                            }

                            if (deleteSourceImage.Checked &&
                                File.Exists(InDirectoryBitmap + Path.DirectorySeparatorChar + InFileBitmap) &&
                                File.Exists(OutDirectoryBitmap + Path.DirectorySeparatorChar + OutFileBitmap))
                                File.Delete(InDirectoryBitmap + Path.DirectorySeparatorChar + InFileBitmap);
                        }
                    }

                    // Delete the source archive now
                    if (deleteSourceArchive.Checked && File.Exists(fileList[i]))
                    {
                        File.Delete(fileList[i]);
                        if (extractDirSameFilename.Checked)
                        {
                            // If the source and destination directory are on the same volume we can just move the directory
                            if (Directory.GetDirectoryRoot(OutDirectory) == Directory.GetDirectoryRoot(fileList[i]))
                                Directory.Move(OutDirectory, fileList[i]);
                            // Otherwise we have to do a series of complicated file moves
                            else
                                MoveDirectory(new DirectoryInfo(OutDirectory), new DirectoryInfo(fileList[i]));
                        }
                    }
                }
                catch
                {
                    // Something went wrong. Continue please.
                    continue;
                }
            }

            // Close the status box now
            status.Close();
            this.Close();
        }