Exemplo n.º 1
0
        public static string GetFileExtension(Stream data)
        {
            // Check to see if the file is an archive
            Archive archive = new Archive(data, null);
            if (archive.Format != ArchiveFormat.NULL)
                return archive.FileExtension;

            // Check to see if the file is an image
            Images images = new Images(data, null);
            if (images.Format != GraphicFormat.NULL)
                return images.FileExtension;

            // Check to see if the file is an ADX (special case)
            if (data.Length > 4 && data.ReadUShort(0x00) == 0x0080 &&
                data.Length > data.ReadUShort(0x02).SwapEndian() + 4 &&
                data.ReadString(data.ReadUShort(0x02).SwapEndian() - 0x02, 6, true) == "(c)CRI")
                return ".adx";

            // Unknown
            return String.Empty;
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
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;
            }
        }
Exemplo n.º 4
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();
        }