private Bitmap LoadImage(ref Stream data, string filename, Stream palette) { try { /* Get and return the image */ Images imageClass = new Images(data, filename); if (imageClass.Format != GraphicFormat.NULL) { imageClass.Decoder.PaletteData = palette; return imageClass.Unpack(); } throw new Exception(); } catch { return null; } }
private Bitmap LoadImage(ref Stream data, string filename) { try { /* Get and return the image */ Images imageClass = new Images(data, filename); try { return imageClass.Unpack(); } catch (GraphicFormatNeedsPalette) { throw new GraphicFormatNeedsPalette(); } } catch (GraphicFormatNeedsPalette) { throw new GraphicFormatNeedsPalette(); } catch { return null; } }
/* Extract files */ private void extract(object sender, EventArgs e) { /* Set the files to extract */ List<int> files = new List<int>(); if (sender == extractSelectedFiles) // Select only the files chosen { for (int i = 0; i < fileListView.SelectedIndices.Count; i++) { if (level == 0 || fileListView.SelectedIndices[i] > 0) files.Add(fileListView.SelectedIndices[i]); } } else // Select all the files { for (int i = (level == 0 ? 0 : 1); i < fileListView.Items.Count; i++) files.Add(i); } /* Don't continue if no files were selected */ if (files.Count == 0) return; /* Display the save file dialog or folder dialog */ string output_directory = String.Empty; string output_filename = String.Empty; /* Directory selection if there are more than one file */ if (files.Count > 1) { output_directory = FileSelectionDialog.SaveDirectory("Select a directory to output the files to"); /* Make sure a directory was selected */ if (output_directory == null || output_directory == String.Empty) return; } for (int i = 0; i < files.Count; i++) { try { string filename = FileList[level].Entries[files[i] - (level > 0 ? 1 : 0)].Filename; int num = files[i] - (level > 0 ? 1 : 0); output_filename = (filename == String.Empty ? num.ToString().PadLeft(FileList.Count.Digits(), '0') : (addFileNumberExtracted.Checked ? Path.GetFileNameWithoutExtension(filename) + '_' + num.ToString().PadLeft(FileList.Count.Digits(), '0') + Path.GetExtension(filename) : filename)); /* Which selection dialog do we want? */ if (files.Count == 1) { output_filename = FileSelectionDialog.SaveFile("Extract File", output_filename, (Path.GetExtension(filename) == String.Empty ? "All Files (*.*)|*.*" : Path.GetExtension(filename).Substring(1).ToUpper() + " File (*" + Path.GetExtension(filename) + ")|*" + Path.GetExtension(filename))); /* Make sure we selected a file */ if (output_filename == null || output_filename == String.Empty) return; output_directory = Path.GetDirectoryName(output_filename); output_filename = Path.GetFileName(output_filename); } /* If for some reason the output directory doesn't exist, create it */ if (!Directory.Exists(output_directory)) Directory.CreateDirectory(output_directory); /* Copy the data to a new stream */ MemoryStream outputData = ArchiveData[level].Copy(FileList[level].Entries[num].Offset, FileList[level].Entries[num].Length); /* Do we want to decompress the file? */ if (decompressExtracted.Checked) { Compression compression = new Compression(outputData, output_filename); if (compression.Format != CompressionFormat.NULL) { MemoryStream decompressedData = compression.Decompress(); if (decompressedData != null) { outputData = decompressedData; output_filename = compression.DecompressFilename; } } } // Convert the file to a PNG? if (extractImageAsPng.Checked) { Images images = new Images(outputData, output_filename); if (images.Format != GraphicFormat.NULL) { Bitmap imageData; try { imageData = images.Unpack(); } catch (GraphicFormatNeedsPalette) { // See if the file exists in the archive int index = indexOfFile(images.PaletteFilename); if (index != -1) images.Decoder.PaletteData = ArchiveData[level].Copy(FileList[level].Entries[index].Offset, FileList[level].Entries[index].Length); imageData = images.Unpack(); } // Now output the image if it was written if (imageData != null) { outputData = new MemoryStream(); imageData.Save(outputData, ImageFormat.Png); output_filename = Path.GetFileNameWithoutExtension(output_filename) + ".png"; } } } /* Write the file */ using (FileStream outputStream = new FileStream(output_directory + Path.DirectorySeparatorChar + output_filename, FileMode.Create, FileAccess.Write)) outputStream.Write(outputData); } catch { continue; } } }
/* Decompress 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 < files.Length; i++) { /* Set the current file */ status.CurrentFile = i; try { /* Open up the file */ MemoryStream data; string outputDirectory, outputFilename; using (FileStream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read)) { /* Set up the decompressor */ Compression compression = new Compression(inputStream, Path.GetFileName(fileList[i])); if (compression.Format == CompressionFormat.NULL) continue; /* Set up the output directories and file names */ outputDirectory = Path.GetDirectoryName(fileList[i]) + (decompressSameDir.Checked ? String.Empty : Path.DirectorySeparatorChar + compression.OutputDirectory); outputFilename = (useStoredFilename.Checked ? compression.DecompressFilename : Path.GetFileName(fileList[i])); /* Decompress data */ MemoryStream decompressedData = compression.Decompress(); /* Check to make sure the decompression was successful */ if (decompressedData == null) continue; else data = decompressedData; } /* Create the output directory if it does not exist */ if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory); /* Write file data */ using (FileStream outputStream = new FileStream(outputDirectory + Path.DirectorySeparatorChar + outputFilename, FileMode.Create, FileAccess.Write)) outputStream.Write(data); /* Delete source file? */ if (deleteSourceFile.Checked && File.Exists(fileList[i])) File.Delete(fileList[i]); /* Unpack image? */ if (unpackImage.Checked) { /* Create Image object and make sure the format is supported */ Images images = new Images(data, outputFilename); if (images.Format != GraphicFormat.NULL) { /* Set up our input and output image */ string inputImage = outputDirectory + Path.DirectorySeparatorChar + outputFilename; string outputImage = outputDirectory + Path.DirectorySeparatorChar + (convertSameDir.Checked ? String.Empty : images.OutputDirectory + Path.DirectorySeparatorChar) + Path.GetFileNameWithoutExtension(outputFilename) + ".png"; // Convert image Bitmap imageData = null; try { imageData = images.Unpack(); } catch (GraphicFormatNeedsPalette) { // See if the palette file exists if (File.Exists(outputDirectory + Path.DirectorySeparatorChar + images.PaletteFilename)) { using (FileStream paletteData = new FileStream(outputDirectory + Path.DirectorySeparatorChar + images.PaletteFilename, FileMode.Open, FileAccess.Read)) { images.Decoder.PaletteData = paletteData; imageData = images.Unpack(); } } } /* Make sure an image was written */ if (imageData != null) { data = new MemoryStream(); imageData.Save(data, ImageFormat.Png); /* Create the output directory if it does not exist */ if (!Directory.Exists(Path.GetDirectoryName(outputImage))) Directory.CreateDirectory(Path.GetDirectoryName(outputImage)); /* Output the image */ using (FileStream outputStream = new FileStream(outputImage, FileMode.Create, FileAccess.Write)) outputStream.Write(data); /* Delete the source image if we want to */ if (deleteSourceImage.Checked && File.Exists(inputImage) && File.Exists(outputImage)) File.Delete(inputImage); } } } } catch { /* Something went wrong. Continue please. */ continue; } } /* Close the status box now */ status.Close(); this.Close(); }
// 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(); }