private static int CompressBest(string infile, MemoryStream output, out Formats actualFormat, params Formats[] formats) { // only read the input data once from the file. byte[] inputData; using (FileStream inStream = File.OpenRead(infile)) { inputData = new byte[inStream.Length]; inStream.Read(inputData, 0, inputData.Length); } MemoryStream bestOutput = null; int minCompSize = int.MaxValue; actualFormat = Formats.GBA; foreach (Formats format in formats) { #region compress the file in each format, and save the best one MemoryStream currentOutput = new MemoryStream(); CompressionFormat realFormat = null; switch (format) { case Formats.HUFF4: realFormat = new Huffman4(); break; case Formats.HUFF8: realFormat = new Huffman8(); break; case Formats.LZ10: realFormat = new LZ10(); break; case Formats.LZ11: realFormat = new LZ11(); break; case Formats.LZOVL: realFormat = new LZOvl(); break; case Formats.RLE: realFormat = new RLE(); break; default: Console.WriteLine("Unsupported single format: " + format); continue; } int currentOutSize; try { using (MemoryStream inStream = new MemoryStream(inputData)) { currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput); } } catch (InputTooLargeException i) { Console.WriteLine(i.Message); actualFormat = format; return(-1); } catch (Exception) { continue; } if (currentOutSize < minCompSize) { bestOutput = currentOutput; minCompSize = currentOutSize; actualFormat = format; } #endregion } if (bestOutput == null) { Console.WriteLine("The file could not be compressed in any format."); return(-1); } bestOutput.WriteTo(output); return(minCompSize); }
private static long Decompress(MemoryStream inputStream, MemoryStream output, CompressionFormat format) { if (!format.Supports(inputStream, inputStream.Length)) { return(-1); } try { return(format.Decompress(inputStream, inputStream.Length, output)); } catch (TooMuchInputException e) { Console.WriteLine(e.Message); return(output.Length); } catch (Exception e) { Console.WriteLine("Could not decompress using the " + format.ShortFormatString + " format; " + e.Message); return(-1); } }
private static int CompressBest(string infile, MemoryStream output, out FormatCompress actualFormat, params FormatCompress[] formats) { // only read the input data once from the file. byte[] inputData; using (FileStream inStream = File.OpenRead(infile)) { inputData = new byte[inStream.Length]; inStream.Read(inputData, 0, inputData.Length); } MemoryStream bestOutput = null; int minCompSize = int.MaxValue; actualFormat = FormatCompress.GBA; foreach (FormatCompress format in formats) { #region compress the file in each format, and save the best one MemoryStream currentOutput = new MemoryStream(); CompressionFormat realFormat = null; switch (format) { case FormatCompress.HUFF4: Huffman.CompressBlockSize = Huffman.BlockSize.FOURBIT; realFormat = new Huffman(); break; case FormatCompress.HUFF8: Huffman.CompressBlockSize = Huffman.BlockSize.EIGHTBIT; realFormat = new Huffman(); break; case FormatCompress.LZ10: realFormat = new LZ10(); break; case FormatCompress.LZ11: realFormat = new LZ11(); break; case FormatCompress.LZOVL: realFormat = new LZOvl(); break; case FormatCompress.RLE: realFormat = new RLE(); break; } int currentOutSize; try { using (MemoryStream inStream = new MemoryStream(inputData)) { currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput); } } catch (InputTooLargeException i) { Console.WriteLine(i.Message); actualFormat = format; return(-1); } catch (Exception) { continue; } if (currentOutSize < minCompSize) { bestOutput = currentOutput; minCompSize = currentOutSize; actualFormat = format; } #endregion } if (bestOutput == null) { Console.WriteLine(Main.Get_Traduction("S1A")); return(-1); } bestOutput.WriteTo(output); return(minCompSize); }