/// <summary> /// Сжатие произвольных файлов /// </summary> public void Deflate(string in_filename, string out_filename, bool enableNewCode = true) { if (!File.Exists(in_filename)) { throw new Exception("Input file not found!"); } using (FileStream fileReader = File.Open(in_filename, FileMode.Open)) { MemoryTributary memOutBuffer; using (MemoryTributary memBuffer = new MemoryTributary()) { fileReader.CopyTo(memBuffer); bool success = Deflate(memBuffer, out memOutBuffer); if (!success) { throw new Exception("Deflate error!"); } using (FileStream fileWriter = new FileStream(out_filename, FileMode.Create)) { memOutBuffer.Position = 0; memOutBuffer.CopyTo(fileWriter); } memOutBuffer.Close(); } } }
/// <summary> /// Сжатие /// </summary> /// <param name="pDataStream"></param> /// <param name="outBufStream"></param> /// <returns></returns> public static bool Deflate(MemoryTributary pDataStream, out MemoryTributary outBufStream) { bool result = true; int DataSize = (int)pDataStream.Length; outBufStream = new MemoryTributary(); pDataStream.Position = 0; try { MemoryTributary srcMemStream = pDataStream; { using (MemoryTributary compressedMemStream = new MemoryTributary()) { using (System.IO.Compression.DeflateStream strmDef = new System.IO.Compression.DeflateStream(compressedMemStream, System.IO.Compression.CompressionMode.Compress)) { srcMemStream.CopyTo(strmDef); } outBufStream = compressedMemStream; } } } catch (Exception ex) { outBufStream = pDataStream; result = false; } return(result); }
public static Tree Get_treeFromV8file(V8File f) { //TBytesStream* sb; Encoding enc; Byte[] bytes = new Byte[0x1000]; Int32 offset; Tree rt; //MemoryStream sb = new MemoryStream(bytes); MemoryTributary sb = new MemoryTributary(bytes); //f.SaveToStream(sb); f.SaveToStream(sb); enc = null; /* * //offset = Encoding::GetBufferEncoding(sb->GetBytes(), enc); * offset = Encoding.GetEncoding() * if (offset == 0) * { * msreg_g.AddError("Ошибка определения кодировки файла контейнера", * "Файл", f->GetFullName()); * delete sb; * return nullptr; * } * bytes = TEncoding::Convert(enc, TEncoding::Unicode, sb->GetBytes(), offset, sb->GetSize() - offset); * * rt = parse_1Ctext(String((WCHART*)&bytes[0], bytes.size() / 2), f->GetFullName()); * delete sb; */ return(null); }
public bool Save_supplier_configs(UInt32 numcon, String filename) { FileStream _fs = null; Container_file f; TableFile tf; if (numcon >= Supplier_configs.Capacity) { return(false); } tf = Supplier_configs[(int)numcon].File; f = new Container_file(tf, tf.Name); if (!f.open()) { //f = null; return(false); } try { _fs = new FileStream(filename, FileMode.OpenOrCreate); } catch { return(false); } MemoryTributary out_fs = new MemoryTributary(); try { Inflate((MemoryTributary)f.Stream, out out_fs); out_fs.CopyTo(_fs); } catch { /* * msreg_m.AddError("Ошибка распаковки файла конфигурации поставщика", * "Имя файла", _filename); */ _fs.Dispose();; return(false); } _fs.Dispose(); return(true); }
/// <summary> /// Распаковка /// </summary> /// <param name="compressedMemoryStream"></param> /// <param name="outBufStream"></param> /// <returns></returns> public static bool Inflate(MemoryTributary compressedMemoryStream, out MemoryTributary outBufStream) { bool result = true; outBufStream = new MemoryTributary(); try { compressedMemoryStream.Position = 0; System.IO.Compression.DeflateStream decompressStream = new System.IO.Compression.DeflateStream(compressedMemoryStream, System.IO.Compression.CompressionMode.Decompress); decompressStream.CopyTo(outBufStream); } catch (Exception ex) { outBufStream = compressedMemoryStream; result = false; } return(result); }
public Stream ReadDataBlock(int start) { Stream stream; Stream stream3; DeflateStream deflateStream; MemoryTributary stream2; MemoryTributary stream_src = new MemoryTributary(); if (start is 0) { return(new MemoryStream()); } int rlen = 0; stream = Read_Block(Data, start, ref rlen); //stream.CopyTo(stream_src); if (zipped) { stream2 = new MemoryTributary(); stream3 = new MemoryStream(); stream.Seek(0, SeekOrigin.Begin); deflateStream = new DeflateStream(stream, CompressionMode.Decompress); deflateStream.CopyTo(stream3); //Inflate(stream_src, out stream2); stream.Dispose(); } else { stream3 = stream; } return(stream3); }
public bool open() { const int MAX_PATH = 260; Stream ts; string tn = ""; char[] tempfile = new char[MAX_PATH]; int i; V8Table t; //table_blob_file addr; // TODO: возможно это должен быть МАССИВ !!!!!!!!!!!!!!!!!!!!!!!! table_blob_file[] addr; // TODO: возможно это должен быть МАССИВ !!!!!!!!!!!!!!!!!!!!!!!! uint maxpartno; if (stream != null) { stream.Seek(0, SeekOrigin.Begin); return(true); } t = file.T; addr = file.addr; maxpartno = file.maxpartno; if (maxpartno > 0) { tn = Path.GetTempFileName(); stream = new FileStream(tn, FileMode.CreateNew); fname = tn; } else { stream = new MemoryStream(); } if (packed == table_file_packed.unknown) { packed = isPacked() ? table_file_packed.yes : table_file_packed.no; } if (rstream != null) { if (packed == table_file_packed.yes) { ts = rstream; } else { stream = rstream; stream.Seek(0, SeekOrigin.Begin); return(true); } } else { if (packed == table_file_packed.yes) { if (maxpartno > 0) { //GetTempFileName(temppath, L"awa", 0, tempfile); //tn = tempfile; //ts = new TFileStream(tn, fmCreate); tn = Path.GetTempFileName(); ts = new FileStream(tn, FileMode.CreateNew); fname = tn; } else { ts = new MemoryStream(); } } else { ts = stream; } for (i = 0; i <= maxpartno; ++i) { t.ReadBlob(ts, addr[i].Blob_start, addr[i].Blob_length, false); } } if (packed == table_file_packed.yes) { ts.Seek(0, SeekOrigin.Begin); MemoryTributary out_stream = new MemoryTributary(); Inflate((MemoryTributary)ts, out out_stream); out_stream.CopyTo(stream); if (rstream is null) { ts.Dispose(); System.IO.File.Delete(tn); } } stream.Seek(0, SeekOrigin.Begin); return(true); }