public string GetCodecPrivate() { if (CodecPrivateRaw == null || CodecPrivateRaw.Length == 0) { return(string.Empty); } if (ContentEncodingType == ContentEncodingTypeCompression && (ContentEncodingScope & ContentEncodingScopePrivateDate) > 0 && // second bit set = private data encoded CodecPrivateRaw.Length > 2) { var outStream = new MemoryStream(); var outZStream = new zlib.ZOutputStream(outStream); var inStream = new MemoryStream(CodecPrivateRaw); try { CopyStream(inStream, outZStream); var buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); return(Encoding.UTF8.GetString(buffer)); } catch { return(Encoding.UTF8.GetString(CodecPrivateRaw)); } finally { outZStream.Close(); inStream.Close(); } } return(Encoding.UTF8.GetString(CodecPrivateRaw)); }
public byte[] Decompress(Stream input) { using (var output = new MemoryStream()) using (var zipStream = new zlib.ZOutputStream(output)) { using (input) { var buffer = new byte[2000]; int len; while ((len = input.Read(buffer, 0, 2000)) > 0) { zipStream.Write(buffer, 0, len); } } // reset output stream to start so we can read it to a string output.Position = 0; byte[] content = new byte[output.Length]; output.Read(content, 0, (int)output.Length); return content; } }
private void CompressZlib(VFile vFile) { if (vFile.DataCompressed != null) { return; } if (vFile.Type != 0) { return; } if (vFile.Data == null || vFile.Data.Length <= 0) { return; } byte[] compressed = null; try { using MemoryStream input = new MemoryStream(vFile.Data, 0, vFile.Data.Length); using MemoryStream output = new MemoryStream(); using zlib.ZOutputStream compressionStream = new zlib.ZOutputStream( output, zlib.zlibConst.Z_DEFAULT_COMPRESSION ); input.Position = 0; byte[] buffer = new byte[2000]; int len; while ((len = input.Read(buffer, 0, 2000)) > 0) { compressionStream.Write(buffer, 0, len); } compressionStream.Flush(); compressionStream.finish(); compressed = new byte[output.Length]; output.Position = 0; output.Read(compressed, 0, compressed.Length); } catch (Exception ex) { Logger.Exception(ex); } if (compressed == null) { Logger.Error($"Failed to compress. (Name:{vFile.Name}"); vFile.DataLengthUncompressed = 0; vFile.DataLengthCompressed = 0; vFile.DataCompressed = null; return; } vFile.DataLengthUncompressed = (uint)vFile.Data.Length; vFile.DataLengthCompressed = (uint)compressed.Length; vFile.DataCompressed = compressed; }
public static byte[] Compress(byte[] input) { System.IO.MemoryStream os = new System.IO.MemoryStream(); zlib.ZOutputStream zos = new zlib.ZOutputStream(os, zlib.zlibConst.Z_BEST_COMPRESSION); zos.Write(input, 0, input.Length); zos.finish(); zos.Close(); return os.ToArray(); }
public static byte[] Compress(byte[] input) { System.IO.MemoryStream os = new System.IO.MemoryStream(); zlib.ZOutputStream zos = new zlib.ZOutputStream(os, zlib.zlibConst.Z_BEST_COMPRESSION); zos.Write(input, 0, input.Length); zos.finish(); zos.Close(); return(os.ToArray()); }
public static byte[] Compress(byte[] data) { MemoryStream ms = new MemoryStream(); zlib.ZOutputStream ds = new zlib.ZOutputStream(ms,3); ds.Write(data, 0, data.Length); ds.Flush(); ds.Close(); return ms.ToArray(); }
public FlashInfo(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException(filename); } FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader reader = new BinaryReader(stream); try { if (stream.Length < 8) { throw new InvalidDataException("文件不是 Flash 文件格式"); } string flashMark = new string(reader.ReadChars(3)); if (flashMark != "FWS" && flashMark != "CWS") { throw new InvalidDataException("文件不是 Flash 文件格式"); } isCompressed = flashMark == "CWS"; version = Convert.ToInt32(reader.ReadByte()); fileLength = reader.ReadInt32(); byte[] dataPart = new byte[stream.Length - 8]; reader.Read(dataPart, 0, dataPart.Length); MemoryStream dataStream = new MemoryStream(dataPart); try { if (isCompressed) { //midified by nasdaqhe MemoryStream outStream = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream); CopyStream(dataStream, outZStream); outStream.Position = 0; ProcessCompressedPart(outStream); outZStream.Close(); outStream.Close(); } else { ProcessCompressedPart(dataStream); } } finally { dataStream.Close(); } } finally { reader.Close(); stream.Close(); } }
public static byte[] Compress(byte[] data) { MemoryStream ms = new MemoryStream(); zlib.ZOutputStream ds = new zlib.ZOutputStream(ms, 3); ds.Write(data, 0, data.Length); ds.Flush(); ds.Close(); return(ms.ToArray()); }
public static void DecompressStreamToStream(Stream inStream, Stream outStream) { zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream); try { CopyStream(inStream, outZStream); } finally { outZStream.Close(); } }
public static byte[] Decompress(byte[] buf) { MemoryStream msOut = new MemoryStream(); MemoryStream msIn = new MemoryStream(); msIn.Write(buf, 0, buf.Length); msIn.Position = 0; zlib.ZOutputStream outZStream = new zlib.ZOutputStream(msOut); CopyStream(msIn, outZStream); return(msOut.ToArray()); }
public static Byte[] Decompress(Byte[] data) { using (var decompressedDataStream = new MemoryStream()) using (var zOutputStream = new zlib.ZOutputStream(decompressedDataStream)) { zOutputStream.Write(data, 0, data.Length); zOutputStream.Flush(); zOutputStream.finish(); return(decompressedDataStream.ToArray()); } }
public static Byte[] Compress(Byte[] data) { using (var compressedDataStream = new MemoryStream()) using (var zOutputStream = new zlib.ZOutputStream(compressedDataStream, zlib.zlibConst.Z_BEST_COMPRESSION)) //using (var zOutputStream = new zlib.ZOutputStream(compressedDataStream, 9)) { zOutputStream.Write(data, 0, data.Length); zOutputStream.Flush(); zOutputStream.finish(); return(compressedDataStream.ToArray()); } }
public static void DecompressFileToStream(string inFile, Stream outStream) { zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); inFileStream.Close(); } }
public static void CompressFileToStream(string inFile, Stream outStream) { zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); inFileStream.Close(); } }
private static byte[] Decompress(byte[] data) { using (var compressedStream = new MemoryStream(data)) { using (var outZStream = new zlib.ZOutputStream(compressedStream, 6)) { using (var resultStream = new MemoryStream()) { CopyStream(resultStream, outZStream); return(resultStream.ToArray()); } } } }
/// <summary> /// Writes MLArrays into <c>OutputStream</c> /// </summary> /// <remarks> /// Writes MAT-file header and compressed data (<c>miCompressed</c>). /// </remarks> /// <param name="stream"><c>Stream</c></param> /// <param name="data"><c>Collection</c> of <c>MLArray</c> elements.</param> /// <param name="compress">Use data compression?</param> public MatFileWriter(BinaryWriter stream, ICollection data, bool compress) { // Write header WriteHeader(stream); foreach (MLArray matrix in data) { if (compress) { // Prepare buffer for MATRIX data MemoryStream memstrm = new MemoryStream(); BinaryWriter bw = new BinaryWriter(memstrm); // Write MATRIX bytes into buffer WriteMatrix(bw, matrix); // Compress data to save storage memstrm.Position = 0; // Rewind the stream MemoryStream compressed = new MemoryStream(); zlib.ZOutputStream zos = new zlib.ZOutputStream(compressed, zlib.zlibConst.Z_DEFAULT_COMPRESSION); byte[] input = new byte[128]; int len = 0; while ((len = memstrm.Read(input, 0, input.Length)) > 0) { zos.Write(input, 0, len); } zos.Flush(); zos.Close(); // important to note that the zlib.ZOutputStream needs to close before writting out the data to the Base.stream //write COMPRESSED tag and compressed data into output channel byte[] compressedBytes = compressed.ToArray(); ByteBuffer buf = new ByteBuffer(2 * 4 + compressedBytes.Length); buf.PutInt(MatDataTypes.miCOMPRESSED); buf.PutInt(compressedBytes.Length); buf.Put(compressedBytes, 0, compressedBytes.Length); stream.Write(buf.Array()); compressed.Close(); } else { // Write MATRIX bytes into buffer WriteMatrix(stream, matrix); } } stream.Close(); }
public static void CompressStreamToFile(Stream inStream, string outFile) { System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.OpenOrCreate); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream, zlib.zlibConst.Z_BEST_COMPRESSION); try { CopyStream(inStream, outZStream); } finally { inStream.Close(); outZStream.Close(); } }
static public string XmlToCode(string xmlStr) { byte[] bytes; using (var outputStream = new System.IO.MemoryStream()) { using (zlib.ZOutputStream compressionStream = new zlib.ZOutputStream(outputStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION)) { byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(xmlStr); compressionStream.Write(byteArray, 0, byteArray.Length); compressionStream.Flush(); } bytes = outputStream.ToArray(); } return(System.Convert.ToBase64String(bytes).Replace("+", "-").Replace("/", "_")); }
public static void CompressStream(Stream inStream, out Stream outStream) { outStream = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); try { CopyStream(inStream, outStream); } catch (Exception ex) { } finally { } }
private void compressFile(string inFile, string outFile) { System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); outFileStream.Close(); inFileStream.Close(); } }
private void decompressFile(string inFile, string outFile) { System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); outFileStream.Close(); inFileStream.Close(); } }
public static void DecompressStream(Stream inStream, out Stream outStream) { outStream = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream); try { CopyStream(inStream, outStream); outZStream.finish(); } catch (Exception ex) { } finally { } }
static public string CodeToXml(string _code) { string code = _code.Replace("-", "+").Replace("_", "/"); byte[] inData = System.Convert.FromBase64String(code); byte[] outData; using (MemoryStream outMemoryStream = new MemoryStream()) { using (zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outMemoryStream)) { outZStream.Write(inData, 0, inData.Length); outZStream.Flush(); } outData = outMemoryStream.ToArray(); } return(System.Text.Encoding.UTF8.GetString(outData)); }
public static bool CompressZlib(byte[] inData, out byte[] outData) { MemoryStream output = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(output, zlib.zlibConst.Z_DEFAULT_COMPRESSION); /* * for (int i = 0, len = inData.Length; i < len; i += 1024) * { * outZStream.Write(inData, i, len - i > 1024 ? 1024 : len - i); * }*/ outZStream.Write(inData, 0, inData.Length); outZStream.Flush(); outZStream.finish(); outData = output.ToArray(); //Core.Unity.Debug.LogError(string.Format("CompressZlib:zzzzzzzz: {0} {1} {2} {3} {4}", inData.Length, outZStream.Length, output.Length, output.Position, outData.Length )); return(true); }
public static byte[] Decompress(byte[] bytes) { MemoryStream stream1 = new MemoryStream(bytes); MemoryStream stream2 = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(stream2); try { CopyStream(stream1, outZStream); } finally { outZStream.Close(); stream1.Close(); stream2.Close(); } return(stream2.ToArray()); }
public static byte[] Deflate(byte[] UncompressedStream) { MemoryStream inStream = new MemoryStream(UncompressedStream); MemoryStream outStream = new MemoryStream(); byte[] outData; zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); try { CopyStream(inStream, outZStream); outData = new byte[outStream.Length]; outStream.Read(outData, 0, (int)outStream.Length); } finally { outZStream.Close(); outStream.Close(); inStream.Close(); } return(outData); }
public static void Save(string filename, int width, int height, byte[] data) { using (var writer = new BinaryWriter(File.Create(filename))) { writer.Write(PNG_SIGNATURE); WriteChunk("IHDR", writer, (chnk) => { chnk.WriteInt32BE(width); chnk.WriteInt32BE(height); chnk.Write((byte)8); chnk.Write((byte)ColorType.Rgba); chnk.Write((byte)0); //Compression method chnk.Write((byte)0); //Filter method chnk.Write((byte)0); //Interlacing }); WriteChunk("IDAT", writer, (chnk) => { //zlib header //deflate compression byte[] buf = new byte[width * 4]; var compress = new zlib.ZOutputStream(chnk.BaseStream, 3); //First line compress.WriteByte((byte)0); for (int x = 0; x < width * 4; x++) { var b = data[width * (height - 1) * 4 + x]; compress.WriteByte(b); } //Filtered lines for (int y = height - 2; y >= 0; y--) { ApplyPaeth(data, (y + 1) * width * 4, y * width * 4, width * 4, buf); compress.WriteByte((byte)4); //paeth filter compress.Write(buf, 0, buf.Length); } compress.finish(); }); writer.Write(IEND); } }
private byte[] compressStream(MemoryStream mem) { MemoryStream outStream = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_BEST_COMPRESSION); byte[] data; try { byte[] buffer = new byte[512]; int len; while ((len = mem.Read(buffer, 0, 512)) > 0) { outZStream.Write(buffer, 0, len); } outZStream.finish(); data = outStream.ToArray(); } finally { outZStream.Close(); outStream.Close(); } return(data); }
private byte[] decompressStream(MemoryStream mem) { byte[] data; using (MemoryStream outStream = new MemoryStream()) using (zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream)) try { byte[] buffer = new byte[512]; int len; while ((len = mem.Read(buffer, 0, 512)) > 0) { outZStream.Write(buffer, 0, len); } outZStream.Flush(); data = outStream.ToArray(); } catch(zlib.ZStreamException ex) { ErrorMsg = ex.Message; return null; } finally { outZStream.finish(); } return data; }
private bool LoadVobSubFromMatroska(MatroskaTrackInfo matroskaSubtitleInfo, MatroskaFile matroska) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show(_language.NoSupportEncryptedVobSub); } ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; var sub = matroska.GetSubtitle(matroskaSubtitleInfo.TrackNumber, MatroskaProgress); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); Cursor.Current = Cursors.Default; MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); List<VobSubMergedPack> mergedVobSubPacks = new List<VobSubMergedPack>(); var idx = new Core.VobSub.Idx(matroskaSubtitleInfo.CodecPrivate.SplitToLines()); foreach (var p in sub) { if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { bool error = false; var outStream = new MemoryStream(); var outZStream = new zlib.ZOutputStream(outStream); var inStream = new MemoryStream(p.Data); byte[] buffer = null; try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { MessageBox.Show(exception.Message + Environment.NewLine + Environment.NewLine + exception.StackTrace); error = true; } finally { outZStream.Close(); inStream.Close(); } if (!error && buffer.Length > 2) mergedVobSubPacks.Add(new VobSubMergedPack(buffer, TimeSpan.FromMilliseconds(p.Start), 32, null)); } else { mergedVobSubPacks.Add(new VobSubMergedPack(p.Data, TimeSpan.FromMilliseconds(p.Start), 32, null)); } if (mergedVobSubPacks.Count > 0) mergedVobSubPacks[mergedVobSubPacks.Count - 1].EndTime = TimeSpan.FromMilliseconds(p.End); // fix overlapping (some versions of Handbrake makes overlapping time codes - thx Hawke) if (mergedVobSubPacks.Count > 1 && mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime > mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime) mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime = TimeSpan.FromMilliseconds(mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime.TotalMilliseconds - 1); } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(mergedVobSubPacks, idx.Palette, Configuration.Settings.VobSubOcr, null); // TODO: language??? if (_loading) { formSubOcr.Icon = (Icon)Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { ResetSubtitle(); _subtitle.Paragraphs.Clear(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(matroska.Path); _converted = true; Text = Title; Configuration.Settings.Save(); return true; } } return false; }
private bool LoadBluRaySubFromMatroska(MatroskaTrackInfo matroskaSubtitleInfo, MatroskaFile matroska) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show(_language.NoSupportEncryptedVobSub); } ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; var sub = matroska.GetSubtitle(matroskaSubtitleInfo.TrackNumber, MatroskaProgress); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); Cursor.Current = Cursors.Default; int noOfErrors = 0; string lastError = string.Empty; MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); var subtitles = new List<BluRaySupParser.PcsData>(); var log = new StringBuilder(); foreach (var p in sub) { byte[] buffer = null; if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { var outStream = new MemoryStream(); var outZStream = new zlib.ZOutputStream(outStream); var inStream = new MemoryStream(p.Data); try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { var tc = new TimeCode(p.Start); lastError = tc + ": " + exception.Message + ": " + exception.StackTrace; noOfErrors++; } finally { outZStream.Close(); inStream.Close(); } } else { buffer = p.Data; } if (buffer != null && buffer.Length > 100) { var ms = new MemoryStream(buffer); var list = BluRaySupParser.ParseBluRaySup(ms, log, true); foreach (var sup in list) { sup.StartTime = (long)((p.Start - 1) * 90.0); sup.EndTime = (long)((p.End - 1) * 90.0); subtitles.Add(sup); // fix overlapping if (subtitles.Count > 1 && sub[subtitles.Count - 2].End > sub[subtitles.Count - 1].Start) subtitles[subtitles.Count - 2].EndTime = subtitles[subtitles.Count - 1].StartTime - 1; } ms.Close(); } else if (subtitles.Count > 0) { var lastSub = subtitles[subtitles.Count - 1]; if (lastSub.StartTime == lastSub.EndTime) { lastSub.EndTime = (long)((p.Start - 1) * 90.0); if (lastSub.EndTime - lastSub.StartTime > 1000000) lastSub.EndTime = lastSub.StartTime; } } } if (noOfErrors > 0) { MessageBox.Show(string.Format("{0} error(s) occurred during extraction of bdsup\r\n\r\n{1}", noOfErrors, lastError)); } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, matroska.Path); if (_loading) { formSubOcr.Icon = (Icon)Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); return true; } } return false; }
private void compressFile(string inFile, string outFile) { System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); outFileStream.Close(); inFileStream.Close(); } }
public virtual Stream OpenInNewStream(Stream a_Stream, FileAccess fA) { bool b0 = true; if (m_Stream != null && (fA & FileAccess.Write) == FileAccess.Write) if (!m_Stream.CanWrite) b0 = false; if (m_Stream != null && b0) return m_Stream; else { a_Stream.Position = Offset; byte[] data = a_Stream.ReadBytes(CompressedSize); if ((this.Compression & RisenPakCompression.RisenPakCompression_ZLib) == RisenPakCompression.RisenPakCompression_ZLib) { b0 = (fA & FileAccess.Write) == FileAccess.Write; MemoryStream mStream = new MemoryStream(UnCompressedSize); zlib.ZOutputStream outp = new zlib.ZOutputStream(mStream); outp.Write(data, 0, data.Length); outp.Position = 0; outp.Flush(); outp.finish(); mStream.Position = 0; return mStream; } else if (((this.Compression & RisenPakCompression.RisenPakCompression_None) == RisenPakCompression.RisenPakCompression_None) || ((this.Compression & RisenPakCompression.RisenPakCompression_Auto) == RisenPakCompression.RisenPakCompression_Auto) ) { MemoryStream mS = new MemoryStream(data.Length); mS.Write(data, 0, data.Length); mS.Position = 0; return mS; } return null; } }
private void decompressFile(string inFile, string outFile) { System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream); System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open); try { CopyStream(inFileStream, outZStream); } finally { outZStream.Close(); outFileStream.Close(); inFileStream.Close(); } }
/// <summary> /// Loads the image /// </summary> public void LoadImg() { BinaryReader binr; fs = new FileStream(path, FileMode.Open); binr = new BinaryReader(fs); int w = 0, h = 0; pixelFormat = s.GetVariable("IMG_PIXEL_FORMAT"); pixelFormat = pixelFormat.ToUpper(); BPP = VarReader.ReadInt32(binr, s, "IMG_BPP"); List <string> order = s.GetVariable("WRITE_ORDER"); for (int q = 0; q < order.Count; q++) { switch (order[q]) { case "FILE_MAGIC": int maglen = s.GetVariable("FILE_MAGIC_LEN"); List <Byte> magic = binr.ReadBytes(maglen).ToList(); List <Byte> magic2 = s.GetVariable("FILE_MAGIC"); //if (magic.Equals(magic2)) // return; break; case "IMG_WIDTH": w = VarReader.ReadInt32(binr, s, order[q]); break; case "IMG_HEIGHT": h = VarReader.ReadInt32(binr, s, order[q]); break; case "IMG_BPP": BPP = VarReader.ReadInt32(binr, s, order[q]); break; case "IMG_PIXELS": if (s.ContainsVariable("IMG_PIXEL_FORMAT")) { byte[] rgb; int size = 0; if (s.ContainsVariable("IMG_UNEVEN_PADDING")) { if (s.GetVariable("IMG_UNEVEN_PADDING")) { int w1 = 0; if (w % 2 > 0) { w1 = 1; } size = (w + w1) * h * (bpc * pixelFormat.Length / 8); } else { size = w * h * (bpc * pixelFormat.Length / 8); } } else { size = w * h * (bpc * pixelFormat.Length / 8); } if (s.ContainsVariable("IMG_PIXELS_LEN")) { size = s.GetVariable("IMG_PIXELS_LEN"); } if (s.ContainsVariable("IMG_PIXELS_LEN_OFFSET")) { binr.BaseStream.Seek(s.GetVariable("IMG_PIXELS_LEN_OFFSET"), SeekOrigin.Begin); size = VarReader.ReadInt32(binr, s, "IMG_PIXELS_LEN"); } if (s.ContainsVariable(order[q] + "_OFFSET")) { binr.BaseStream.Seek(s.GetVariable(order[q] + "_OFFSET"), SeekOrigin.Begin); } rgb = binr.ReadBytes(size); if (s.ContainsVariable("IMG_DEFLATE")) { if (s.GetVariable("IMG_DEFLATE")) { System.IO.FileStream inFile = new System.IO.FileStream("CompZ.bin", System.IO.FileMode.Create); BinaryWriter binZ = new BinaryWriter(inFile); binZ.Write(rgb); //binZ.Flush(); binZ.Close(); inFile.Close(); System.IO.FileStream outFileStream = new System.IO.FileStream("DecompZ.bin", System.IO.FileMode.Create); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream); System.IO.FileStream inFileStream = new System.IO.FileStream("CompZ.bin", System.IO.FileMode.Open); try { byte[] buffer = new byte[2000]; int len; while ((len = inFileStream.Read(buffer, 0, 2000)) > 0) { outZStream.Write(buffer, 0, len); } outZStream.Flush(); } finally { outZStream.Close(); outFileStream.Close(); inFileStream.Close(); } System.IO.FileStream inFile2 = new System.IO.FileStream("DecompZ.bin", System.IO.FileMode.Open); BinaryReader binZ2 = new BinaryReader(inFile2); //rgb = new byte[inFile2.Length]; //inFile2.Read(rgb, 0, (int)inFile2.Length); rgb = binZ2.ReadBytes((int)inFile2.Length); binZ2.Close(); inFile2.Close(); } } if (s.ContainsVariable("IMG_PIXELS_HANDLER")) { if (s.GetVariable("IMG_PIXELS_HANDLER")) { var ProcessPixels = s.GetVariable("ProcessPixels"); IList <object> rgbp = new List <object>(); for (int i = 0; i < rgb.Length; i++) { rgbp.Add(rgb[i]); } List <byte> rgbl = ProcessPixels(rgbp, w, h, BPP); rgb = rgbl.ToArray(); } } img = new Bitmap(w, h); Color[,] pixels = Render(rgb, h, w); for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { img.SetPixel(i, j, pixels[i, j]); } } } break; default: VarReader.ReadInt32(binr, s, order[q]); break; } } binr.Close(); fs.Close(); }
public ZRLECompressedReader(Stream uncompressedStream) : base(uncompressedStream) { zlibMemoryStream = new MemoryStream(); zlibDecompressedStream = new zlib.ZOutputStream(zlibMemoryStream); uncompressedReader = new BinaryReader(zlibMemoryStream); }
private byte[] compressStream(MemoryStream mem) { MemoryStream outStream = new MemoryStream(); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_BEST_COMPRESSION); byte[] data; try { byte[] buffer = new byte[512]; int len; while ((len = mem.Read(buffer, 0, 512)) > 0) { outZStream.Write(buffer, 0, len); } outZStream.finish(); data = outStream.ToArray(); } finally { outZStream.Close(); outStream.Close(); } return data; }
/// <summary> /// Writes MLArrays into <c>OutputStream</c> /// </summary> /// <remarks> /// Writes MAT-file header and compressed data (<c>miCompressed</c>). /// </remarks> /// <param name="stream"><c>Stream</c></param> /// <param name="data"><c>Collection</c> of <c>MLArray</c> elements.</param> /// <param name="compress">Use data compression?</param> public MatFileWriter( BinaryWriter stream, ICollection data, bool compress ) { // Write header WriteHeader( stream ); foreach( MLArray matrix in data ) { if (compress) { // Prepare buffer for MATRIX data MemoryStream memstrm = new MemoryStream(); BinaryWriter bw = new BinaryWriter(memstrm); // Write MATRIX bytes into buffer WriteMatrix(bw, matrix); // Compress data to save storage memstrm.Position = 0; // Rewind the stream MemoryStream compressed = new MemoryStream(); zlib.ZOutputStream zos = new zlib.ZOutputStream(compressed, zlib.zlibConst.Z_DEFAULT_COMPRESSION); byte[] input = new byte[128]; int len = 0; while ((len = memstrm.Read(input, 0, input.Length)) > 0) zos.Write(input, 0, len); zos.Flush(); zos.Close(); // important to note that the zlib.ZOutputStream needs to close before writting out the data to the Base.stream //write COMPRESSED tag and compressed data into output channel byte[] compressedBytes = compressed.ToArray(); ByteBuffer buf = new ByteBuffer(2 * 4 + compressedBytes.Length); buf.PutInt(MatDataTypes.miCOMPRESSED); buf.PutInt(compressedBytes.Length); buf.Put(compressedBytes, 0, compressedBytes.Length); stream.Write(buf.Array()); compressed.Close(); } else { // Write MATRIX bytes into buffer WriteMatrix(stream, matrix); } } stream.Close(); }