您的位置:首页 > 其它

Stream,byte[],LZMA

2015-07-24 14:53 369 查看

//转自 瓦里奥

一. 二进制转换成图片

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

1.

2.

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

四. Stream 和 文件之间的转换

将 Stream 写入文件

五. 从文件读取 Stream



//from stackoverflow

private
static void
CompressFileLZMA(string inFile,
string outFile)
{ SevenZip.Compression.LZMA.Encoder
coder = new
SevenZip.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();
} private
static void
DecompressFileLZMA(string inFile,
string outFile)
{ SevenZip.Compression.LZMA.Decoder
coder = new
SevenZip.Compression.LZMA.Decoder();
FileStream input =
new FileStream(inFile,
FileMode.Open);
FileStream output =
new FileStream(outFile,
FileMode.Create);
// Read the decoder properties
byte[] properties
= new
byte[5]; input.Read(properties,
0,
5); // Read in the decompress file size.
byte [] fileLengthBytes
= new
byte[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 static
void Decompress(Stream inStream,
Stream outStream){
byte[] properties
= new
byte[5]; inStream.Read(properties,
0,
5); SevenZip.Compression.LZMA.Decoder
decoder = new
SevenZip.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);}public
static string
DecompressLzma(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
= new
StreamReader(outputStream))
{ outputStream.Position
= 0;
string output = reader.ReadToEnd();
return output;
} }
return "";}

public static
string CompressLzma(string inputstring){
if (!string.IsNullOrEmpty(inputstring))
{ var stream
= new
MemoryStream(Encoding.Unicode.GetBytes(inputstring
?? ""));
var outputStream =
new MemoryStream();
Compress(stream, outputStream);
byte[] bytes
= outputStream.ToArray();
} return
"";}public
static void
Compress(MemoryStream inStream,
MemoryStream outStream){
CoderPropID[] propIDs;
object[] properties;
PrepareEncoder(out propIDs,
out properties);
SevenZip.Compression.LZMA.Encoder encoder
= new
SevenZip.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);}public
static void
PrepareEncoder(out
CoderPropID[] 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 data
Int32 litPosBits =
0;
// UInt32 litPosBits = 2; // for 32-bit data
Int32 algorithm =
2;
Int32 numFastBytes =
32;
string mf =
"bt2"; propIDs
= new
CoderPropID[]
{ 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 static
string CompressLzma(string inputstring){
if (!string.IsNullOrEmpty(inputstring))
{ var stream
= new
MemoryStream(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
"";}public
static void
PrepareEncoder(out
CoderPropID[] 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 data
Int32 litPosBits =
0;
// UInt32 litPosBits = 2; // for 32-bit data
Int32 algorithm =
2;
Int32 numFastBytes =
64;
string mf =
"bt4"; propIDs
= new
CoderPropID[]
{ 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
};}private
static int signedInt(int unsignedInt){
return unsignedInt >=
128 ?
Math.Abs(128
- unsignedInt)
- 128
: unsignedInt;}public
static void
Compress(MemoryStream inStream,
MemoryStream outStream){
CoderPropID[] propIDs;
object[] properties;
PrepareEncoder(out propIDs,
out properties);
SevenZip.Compression.LZMA.Encoder encoder
= new
SevenZip.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);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: