110,571
社区成员
发帖
与我相关
我的任务
分享
String path = @"C:\\1.xls";
FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
FileStream output;
Byte[] bytes;
if (compress)
{
//压缩
bytes = new Byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
fs.Dispose();
output = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
ZOutputStream zOut = new ZOutputStream(output, zlibConst.Z_DEFAULT_COMPRESSION);
zOut.Write(bytes, 0, bytes.Length);
zOut.finish();
zOut.Close();
}
else
{
//解压缩
bytes = new Byte[0];
ZInputStream zIn = new ZInputStream(fs);
int curPosition=0;
Byte[] data = new Byte[4096];
while (true)
{
int size = zIn.Read(data, 0, data.Length);
if (size > 0)
{
curPosition += size;
Byte[] tempBytes = new Byte[bytes.LongLength + data.Length];
bytes.CopyTo(tempBytes, 0);
data.CopyTo(tempBytes, bytes.LongLength);
bytes = tempBytes;
}
else
{
break;
}
}
fs.Close();
fs.Dispose();
output = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
output.Write(bytes, 0, curPosition);
output.Flush();
}
output.Close();
output.Dispose();