如题,对图形格式的文件没有问题
但是如果是文本文件的话就会出现解压缩后保存的文件内容是乱码?实在不知道如何解决,在网上找了问题也没有。
以下是解压缩代码: private static byte[] DecompressByteData(byte[] dataValue)
{
byte[] resultValue = null;
try
{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
{
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception ex)
{
System.Console.WriteLine("解压缩数据失败!"+ex.Message);
} //返回数据
return resultValue;
} 或者怎样在压缩前就指定文件格式并且一并压缩呢?
但是如果是文本文件的话就会出现解压缩后保存的文件内容是乱码?实在不知道如何解决,在网上找了问题也没有。
以下是解压缩代码: private static byte[] DecompressByteData(byte[] dataValue)
{
byte[] resultValue = null;
try
{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
{
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception ex)
{
System.Console.WriteLine("解压缩数据失败!"+ex.Message);
} //返回数据
return resultValue;
} 或者怎样在压缩前就指定文件格式并且一并压缩呢?
dataValue是压缩以后的数据吧?
{
byte[] resultValue = null;
try
{
long m = dataValue.Length;
//压缩数据
Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
compressFile.SetInput(dataValue);
compressFile.Finish();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!compressFile.IsFinished)
{
int bufLength = compressFile.Deflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close(); }
catch (Exception e)
{
System.Console.WriteLine("压缩数据失败!" + e.Message);
} //返回压缩后的数据
long n = resultValue.Length;
return resultValue;}这是压缩的方法
如果我要加入编码命令如何解决呢?
真抱歉打扰大家
我想在这个前面把httppostfile文件转化成utf8的然后再变成httppostfile对象能否实现byte[] b = new byte[oFiles[1].ContentLength];
System.IO.Stream fs = (System.IO.Stream)oFiles[1].InputStream;
fs.Read(b, 0, oFiles[1].ContentLength);
你能否测试oFiles[1]直接保存以后的结果,应该和上传文件一致。
Encoding gb2312 = Encoding.GetEncoding("gb2312");
FileStream fs = new FileStream("g:\\temp\\gb2312.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, gb2312);
MemoryStream ms = new MemoryStream();
string line = null;
byte[] buf;
while ((line = sr.ReadLine()) != null)
{
buf = gb2312.GetBytes(line + "\r\n");
ms.Write(buf, 0, buf.Length);
}
ms.Close();
sr.Close();
fs.Close();int bytes;
Deflater deflater = new Deflater();
deflater.SetInput(ms.ToArray());
buf = new byte[1024];
ms = new MemoryStream();
while (!deflater.IsNeedingInput)
{
bytes = deflater.Deflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
deflater.Finish();
while (!deflater.IsFinished)
{
bytes = deflater.Deflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
ms.Close();Inflater inflater = new Inflater();
inflater.SetInput(ms.ToArray());
ms = new MemoryStream();
while (!inflater.IsFinished)
{
bytes = inflater.Inflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
ms.Close();buf = ms.ToArray();
fs = new FileStream("g:\\temp\\gb2312.txt.bak", FileMode.Create);
fs.Write(buf, 0, buf.Length);
fs.Close();gb2312.txt内容
好好学习天天向上。
锄禾日当午。
火星。程序运行结果gb2312.txt和gb2312.txt.bak内容一致。
string FilePath = oFiles[1].FileName;
System.IO.Stream fs = (System.IO.Stream)oFiles[1].InputStream;
fs.Read(b, 0, oFiles[1].ContentLength);
byte[] bb = CompressByteData(b); //对二进制数组进行压缩upfile(bb); 方法调用进行保存
FileStream fs = new FileStream("g:\\temp\\gb2312.txt", FileMode.Open);
MemoryStream ms = new MemoryStream();
string line = null;
byte[] buf = byte[1024];
int bytes;
while ((bytes = fs.Read(buf, 0, buf.Length)) != 0)
{
ms.Write(buf, 0, bytes);
}
ms.Close();
fs.Close();
这段代码跟文件编码无关直接读二进制流。
你的程序现在是前台页面上传文件,后台程序压缩然后保存,图片压缩保存正常,文本文件出现乱码。
你可以把上传以后的内容直接保存来验证一下是否上传的数据没有问题,我看你压缩解压那里的代码
都没有问题呀。