Stream,byte[],LZMA

来源:互联网 发布:python idle使用技巧 编辑:程序博客网 时间:2024/06/07 00:09


//转自 瓦里奥

一. 二进制转换成图片

1
2
3
4
5
MemoryStream ms =new MemoryStream(bytes);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
this.pictureBox1.Image

 

二. C#中byte[]与string的转换代码

 

1.

1
2
3
System.Text.UnicodeEncoding converter =new System.Text.UnicodeEncoding();
  byte[] inputBytes =converter.GetBytes(inputString);
  stringinputString = converter.GetString(inputBytes);

 

2.

1
2
3
string inputString = System.Convert.ToBase64String(inputBytes);
  byte[] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream =new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

 

三. C# Stream 和 byte[] 之间的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// 将 Stream 转成 byte[]
 
public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes =new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    returnbytes;
}
 
/// 将 byte[] 转成 Stream
 
publicStream BytesToStream(byte[] bytes)
{
    Stream stream =new MemoryStream(bytes);
    returnstream;
}

 

四. Stream 和 文件之间的转换

将 Stream 写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void StreamToFile(Stream stream,stringfileName)
{
    // 把 Stream 转换成 byte[]
    byte[] bytes =new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    // 把 byte[] 写入文件
    FileStream fs =new FileStream(fileName, FileMode.Create);
    BinaryWriter bw =new BinaryWriter(fs);
    bw.Write(bytes);
    bw.Close();
    fs.Close();
}

 

五. 从文件读取 Stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Stream FileToStream(string fileName)
{           
    // 打开文件
    FileStream fileStream =new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    // 读取文件的 byte[]
    byte[] bytes =new byte[fileStream.Length];
    fileStream.Read(bytes, 0, bytes.Length);
    fileStream.Close();
    // 把 byte[] 转换成 Stream
    Stream stream =new MemoryStream(bytes);
    returnstream;
 
 
}

 

1
2
3
4
5
6
//Bitmap 转化为 Byte[]
                Bitmap BitReturn =new Bitmap();
                byte[] bReturn =null;
                MemoryStream ms =new MemoryStream();
                BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                bReturn = ms.GetBuffer();



//from stackoverflow

private static voidCompressFileLZMA(string inFile,string outFile){ SevenZip.Compression.LZMA.Encoder coder = newSevenZip.Compression.LZMA.Encoder();FileStream input =new FileStream(inFile,FileMode.Open);FileStream output =new FileStream(outFile,FileMode.Create);// Write the encoder properties coder.WriteCoderProperties(output);// Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length),0, 8); // Encode the file. coder.Code(input, output, input.Length,-1,null); output.Flush(); output.Close();} privatestatic voidDecompressFileLZMA(string inFile,string outFile){ SevenZip.Compression.LZMA.Decoder coder = newSevenZip.Compression.LZMA.Decoder();FileStream input =new FileStream(inFile,FileMode.Open);FileStream output =new FileStream(outFile,FileMode.Create);// Read the decoder properties byte[] properties= newbyte[5]; input.Read(properties,0, 5); // Read in the decompress file size.byte [] fileLengthBytes= newbyte[8]; input.Read(fileLengthBytes,0, 8); long fileLength= BitConverter.ToInt64(fileLengthBytes,0); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close();}


public staticvoid Decompress(Stream inStream,Stream outStream){byte[] properties= newbyte[5]; inStream.Read(properties,0, 5); SevenZip.Compression.LZMA.Decoder decoder = newSevenZip.Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties);long outSize =0; for (int i= 0; i< 8; i++){ int v= inStream.ReadByte(); outSize|= ((long)(byte)v)<< (8* i);} long compressedSize= inStream.Length- inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize,null);}publicstatic stringDecompressLzma(string inputstring){if (!string.IsNullOrEmpty(inputstring)){ byte[] myInts= Array.ConvertAll(inputstring.Split(','), s => (byte)int.Parse(s));var stream =new MemoryStream(myInts);var outputStream =new MemoryStream();Decompress(stream, outputStream); using(var reader= newStreamReader(outputStream)){ outputStream.Position= 0;string output = reader.ReadToEnd();return output;} }return "";}


public staticstring CompressLzma(string inputstring){if (!string.IsNullOrEmpty(inputstring)){ var stream= newMemoryStream(Encoding.Unicode.GetBytes(inputstring?? ""));var outputStream =new MemoryStream();Compress(stream, outputStream);byte[] bytes= outputStream.ToArray();} return"";}publicstatic voidCompress(MemoryStream inStream,MemoryStream outStream){CoderPropID[] propIDs;object[] properties;PrepareEncoder(out propIDs,out properties);SevenZip.Compression.LZMA.Encoder encoder= newSevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream);Int64 fileSize = inStream.Length;for (int i= 0; i< 8; i++){ outStream.WriteByte((Byte)(fileSize>> (8* i)));} encoder.Code(inStream, outStream,-1,-1,null);}publicstatic voidPrepareEncoder(outCoderPropID[] propIDs,out object[] properties){ bool eos= true;Int32 dictionary =1 <<16; Int32 posStateBits =2; Int32 litContextBits =3; // for normal files // UInt32 litContextBits = 0; // for 32-bit dataInt32 litPosBits =0; // UInt32 litPosBits = 2; // for 32-bit dataInt32 algorithm =2; Int32 numFastBytes =32; string mf ="bt2"; propIDs= newCoderPropID[]{ CoderPropID.DictionarySize,CoderPropID.PosStateBits,CoderPropID.LitContextBits,CoderPropID.LitPosBits,CoderPropID.Algorithm,CoderPropID.NumFastBytes,CoderPropID.MatchFinder,CoderPropID.EndMarker}; properties =new object[]{ dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos};}


public staticstring CompressLzma(string inputstring){if (!string.IsNullOrEmpty(inputstring)){ var stream= newMemoryStream(Encoding.UTF8.GetBytes(inputstring?? ""));var outputStream =new MemoryStream();Compress(stream, outputStream);byte[] bytes= outputStream.ToArray();var result =string.Join(",",Array.ConvertAll(bytes, v=> signedInt((int)v)));return result;} return"";}publicstatic voidPrepareEncoder(outCoderPropID[] propIDs,out object[] properties){ bool eos= true;Int32 dictionary =1 <<16; Int32 posStateBits =2; Int32 litContextBits =3; // for normal files // UInt32 litContextBits = 0; // for 32-bit dataInt32 litPosBits =0; // UInt32 litPosBits = 2; // for 32-bit dataInt32 algorithm =2; Int32 numFastBytes =64; string mf ="bt4"; propIDs= newCoderPropID[]{ CoderPropID.DictionarySize,CoderPropID.PosStateBits,CoderPropID.LitContextBits,CoderPropID.LitPosBits,CoderPropID.Algorithm,CoderPropID.NumFastBytes,CoderPropID.MatchFinder,CoderPropID.EndMarker}; properties =new object[]{ dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos};}privatestatic int signedInt(int unsignedInt){return unsignedInt >=128 ?Math.Abs(128- unsignedInt)- 128: unsignedInt;}publicstatic voidCompress(MemoryStream inStream,MemoryStream outStream){CoderPropID[] propIDs;object[] properties;PrepareEncoder(out propIDs,out properties);SevenZip.Compression.LZMA.Encoder encoder= newSevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream);Int64 fileSize = inStream.Length;for (int i= 0; i< 8; i++){ outStream.WriteByte((Byte)(fileSize>> (8* i)));} encoder.Code(inStream, outStream,-1,-1,null);}

0 0