/// <summary> /// 内部使用,递归添加一个目录中的文件 /// </summary> private void AddDirFileToFileList(string aDir, string aBaseDir) { //增加目录本身 if (!aDir.Equals(aBaseDir)) { I3SharpZipFileInfo fileInfo = new I3SharpZipFileInfo(aDir, aBaseDir, true, I3SZFileInfoMode.szimNew); fileInfoList.Add(fileInfo); } //增加子文件 foreach (string aFileName in Directory.GetFiles(aDir)) { I3SharpZipFileInfo fileInfo = new I3SharpZipFileInfo(aFileName, aBaseDir, false, I3SZFileInfoMode.szimNew); fileInfoList.Add(fileInfo); } //增加子目录 foreach (string aDirName in Directory.GetDirectories(aDir)) { AddDirFileToFileList(aDirName, aBaseDir); } }
/// <summary> /// 压缩单个文件 /// </summary> /// <param name="aFileName"></param> /// <param name="aDestFileName"></param> /// <returns></returns> public I3MsgInfo CompressASingleFile(string aFileName, string aDestFileName) { if (!I3FileUtil.CheckFileNotExists(aDestFileName)) { return(new I3MsgInfo(false, "")); } FileName = aDestFileName; I3SharpZipFileInfo fileInfo = new I3SharpZipFileInfo(Path.GetFileName(aFileName), aFileName, I3FileUtil.GetFileSize(aFileName), I3SZFileInfoMode.szimNew); fileInfoList.Add(fileInfo); I3MsgInfo msg = Flush(); if (!msg.State) { return(msg); } FileName = aDestFileName; return(new I3MsgInfo(fileExists && fileOK, "")); }
/// <summary> /// 解压所有文件到指定的目录 /// /// checkFileIsNormal:解压时,是否在fileInfoList中检查其状态为normal,为normal时才解压 /// 此功能用于更换或者删除文件时,同时,为normal状态的fileinfo.fullName将被更新,指示现在被解压到了哪里 /// /// </summary> /// <param name="aNewPath"></param> /// <returns></returns> public I3MsgInfo UnCompressAllFile(string aNewPath, bool checkFileIsNormal) { if (!fileExists) { return(I3MsgInfo.Default); } I3MsgInfo msg; #region 定义变量 string message = ""; ZipInputStream inputStream = null; int normalFileCount = GetNormalFileCount(); long totalNormalFileSize = GetTotalNormalFileSize(); //所有需要解压的文件的字节数 long passFileSize = 0; //已经解压掉的文件的字节数 long totalPosition = 0; //总共解压到的字节数 #endregion try { inputStream = new ZipInputStream(File.OpenRead(fileName)); if (!string.IsNullOrEmpty(passWord)) { inputStream.Password = passWord; } ZipEntry theEntry; int fileindex = 0; while ((theEntry = inputStream.GetNextEntry()) != null) { I3SharpZipFileInfo normalInfo = null; #region 检查文件是否为normal状态 if (checkFileIsNormal) { bool isNormal = false; foreach (I3SharpZipFileInfo info in fileInfoList) { if (info.FileName.Equals(theEntry.Name)) { isNormal = info.Mode == I3SZFileInfoMode.szimNormal; normalInfo = info; break; } } if (!isNormal) { continue; } } #endregion #region 获取目的文件名,并生成目录 normalInfo不为空时,更新FullName string aNewFileName = Path.Combine(aNewPath, theEntry.Name); if (normalInfo != null) { normalInfo.FullName = aNewFileName; } msg = I3DirectoryUtil.CreateDirectoryByFileName(aNewFileName); if (!msg.State) { return(msg); } #endregion #region 发送文件个数进度信息 message = "正在解压文件" + theEntry.Name + "到" + aNewFileName; fileindex++; OnMutiFileProcessReport(normalFileCount, fileindex, message); #endregion #region 如果是目录则不需要解压 文件大小为0且文件名最后一个字符是/表示是目录 if ((theEntry.Size.Equals(0) && (I3StringUtil.SubString(theEntry.Name, theEntry.Name.Length - 1).Equals("/")))) { continue; } #endregion #region 定义单个文件的变量 FileStream streamWriter = null; #endregion try { #region 解压 streamWriter = File.Create(aNewFileName); byte[] data = new byte[blockSize]; long longsize = 0; while (true) { int size = inputStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); longsize += size; OnSingleFileProcessReport(theEntry.Size, longsize, message); totalPosition = passFileSize + longsize; OnTotalBytesProcessReport(totalNormalFileSize, totalPosition, message); } else { break; } } passFileSize += longsize; #endregion } finally { #region 释放单个文件的变量 if (streamWriter != null) { streamWriter.Close(); } #endregion } } return(I3MsgInfo.Default); } #region 错误处理 catch (Exception ex) { return(new I3MsgInfo(false, ex.Message, ex)); } #endregion #region 释放inputStream finally { if (inputStream != null) { inputStream.Close(); } } #endregion }
/// <summary> /// 根据文件信息从压缩包中获取单个文件,需要指定FullName /// </summary> /// <param name="aFileInfo"></param> /// <returns></returns> public I3MsgInfo UnCompressSingleFile(I3SharpZipFileInfo aFileInfo) { #region 检查文件名与上级目录 if (string.IsNullOrEmpty(aFileInfo.FullName)) { return(new I3MsgInfo(false, "未设置目的路径!")); } I3MsgInfo msg = I3DirectoryUtil.CreateDirectoryByFileName(aFileInfo.FullName); if (!msg.State) { return(msg); } string message = "正在解压文件" + aFileInfo.FileName + "到" + aFileInfo.FullName; #endregion #region 检查是否是目录 文件大小为0且文件名最后一个字符是/表示是目录 if ((aFileInfo.FileSize.Equals(0) && (I3StringUtil.SubString(aFileInfo.FileName, aFileInfo.FileName.Length - 1).Equals("/")))) { return(I3MsgInfo.Default); } #endregion bool result = false; ZipInputStream inputStream = null; try { inputStream = new ZipInputStream(File.OpenRead(fileName)); if (!string.IsNullOrEmpty(passWord)) { inputStream.Password = passWord; } ZipEntry theEntry; while ((theEntry = inputStream.GetNextEntry()) != null) { #region 判断文件名是否为要解压的文件 if (!theEntry.Name.Equals(aFileInfo.FileName)) { continue; } #endregion #region 定义单个文件的变量 FileStream streamWriter = null; #endregion try { #region 解压 streamWriter = File.Create(aFileInfo.FullName); byte[] data = new byte[blockSize]; long longsize = 0; while (true) { int size = inputStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); longsize += size; OnSingleFileProcessReport(aFileInfo.FileSize, longsize, message); } else { break; } } #endregion } finally { #region 释放单个文件的变量 if (streamWriter != null) { streamWriter.Close(); } #endregion } result = true; } } #region 错误处理 catch (Exception ex) { return(new I3MsgInfo(false, ex.Message, ex)); } #endregion #region 释放inputStream finally { if (inputStream != null) { inputStream.Close(); } } #endregion return(new I3MsgInfo(result, "")); }