#region 文件压缩
/// <summary>
/// 文件压缩
/// </summary>
/// <param name="M_str_DFile">压缩前文件及路径</param>
/// <param name="M_str_CFile">压缩后文件及路径</param>
public void compressFile(string M_str_DFile, string M_str_CFile)
{
if (!File.Exists(M_str_DFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(M_str_DFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(M_str_CFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}
#endregion
/// <summary>
/// 文件压缩
/// </summary>
/// <param name="M_str_DFile">压缩前文件及路径</param>
/// <param name="M_str_CFile">压缩后文件及路径</param>
public void compressFile(string M_str_DFile, string M_str_CFile)
{
if (!File.Exists(M_str_DFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(M_str_DFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(M_str_CFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}
#endregion
解压使用GZipStream,WINRAR,ICSharpCode.SharpZipLib
是我传参数弄错了,将目的压缩文件后缀写成了rar所以解压的时候会出错的,但是改过来以后,手动解压出来的文件不是原来文件的后缀格式的,是没有后缀的文件,需要手动改成原来文件的格式,这个有没有办法解决啊?