//public bool ffshowProcess = true; /// <summary> /// 将文件流写入到压缩流中 /// </summary> /// <param name="sourceFileName"></param> /// <param name="zipOutPutStream"></param> /// <param name="sourceFileStream">非完整文件名</param> /// <param name="BlockSize"></param> /// <param name="form"></param> private static void WriteFile(string sourceFileName, ZipOutputStream zipOutPutStream, FileStream sourceFileStream, int BlockSize, IProgressReporter progressReporter) { string messageInfo = "正在压缩文件\"" + sourceFileName + "\""; sourceFileStream.Position = 0; I3Stream.WriteStreamToStream(messageInfo, sourceFileStream, zipOutPutStream, BlockSize, progressReporter); }
/// <summary> /// 使用GZip方式压缩文件 /// </summary> /// <param name="sourceFile"></param> /// <param name="destFile"></param> /// <param name="maxFileMBs"></param> /// <returns></returns> public static I3MsgInfo ZipFile(string sourceFile, string destFile, int maxFileMBs) { if (!File.Exists(sourceFile)) { return(new I3MsgInfo(false, "错误:源文件不存在!文件名:" + sourceFile)); } FileStream sourceStream = null; FileStream destStream = null; GZipStream zipStream = null; try { try { sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); if (sourceStream.Length > maxFileMBs * 1024 * 1024) { return(new I3MsgInfo(false, "要压缩的文件大小超过指定的最大大小" + maxFileMBs.ToString() + "M")); } destStream = new FileStream(destFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None); zipStream = new GZipStream(destStream, CompressionMode.Compress); } catch (Exception ex) { return(new I3MsgInfo(false, ex.Message, ex)); } //sourceStream刚创建,从0开始 return(I3Stream.WriteStreamToStream(null, sourceStream, zipStream, 4096, null)); } finally { if (zipStream != null) { zipStream.Close(); } if (destStream != null) { destStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } } }