Exemple #1
0
        /// <summary>
        /// 将一个文件或一个文件夹及其子文件夹和子文件添加到压缩包内
        /// </summary>
        /// <param name="zipStream">要添加文件的压缩文件输出流</param>
        /// <param name="fileSystem">要添加的文件或文件夹</param>
        /// <param name="rootPath">该文件或文件夹在压缩文件内的父级路径</param>
        public static void AddEntry(ZipOutputStream zipStream, FileSystemInfo fileSystem, string rootPath = "")
        {
            ZipEntry zipEntry = null;

            Console.WriteLine(fileSystem.FullName);
            //如果是文件夹
            if (fileSystem is DirectoryInfo)
            {
                //创建压缩文件内的路劲
                zipEntry = new ZipEntry(Path.Combine(rootPath, fileSystem.Name + "\\"));
                //调用事件通知外部处理事件信息
                OnCompress?.Invoke(fileSystem.Name, 1, 1f, true);
                //将压缩文件内的文件夹路径添加到压缩文件输出流
                zipStream.PutNextEntry(zipEntry);
                //遍历文件夹下的子文件夹,子文件递归添加
                foreach (FileSystemInfo nextFileSystem in (fileSystem as DirectoryInfo).GetFileSystemInfos())
                {
                    AddEntry(zipStream, nextFileSystem, zipEntry.Name);
                }
            }
            //如果是文件
            else
            {
                //创建压缩文件内的路劲
                zipEntry = new ZipEntry(Path.Combine(rootPath, fileSystem.Name));
                //将压缩文件内的文件夹路径添加到压缩文件输出流
                zipStream.PutNextEntry(zipEntry);
                //读取文件数据
                using (FileStream readFile = new FileStream(fileSystem.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    ////将读文件流的索引定位到文件的开头
                    //readFile.Seek(0, SeekOrigin.Begin);
                    //定义一个1k的数据包
                    byte[] readDataBytes = new byte[1024];
                    //定义一个变量存放已读取的数据的总大小
                    uint totalReadNum = 0;
                    //只要读文件流的索引小于文件最大长度就不停的循环读取文件
                    while (readFile.Position < readFile.Length)
                    {
                        try
                        {
                            //读取文件并得到当前实际读取到的数据的大小
                            int readNum = readFile.Read(readDataBytes, 0, readDataBytes.Length);
                            //更新已读取的数据的总大小
                            totalReadNum += (uint)readNum;
                            //调用事件通知外部处理事件信息
                            OnCompress?.Invoke(fileSystem.Name, totalReadNum, readFile.Length, false);
                            //将文件数据写入到压缩文件输出流内
                            zipStream.WriteAsync(readDataBytes, 0, readNum).Wait();
                        }catch (Exception ex)
                        {
                            //有可能会发生文件占用,文件不存在等异常
                            LogUtil.WriteException(ex.ToString());
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 添加一个文件或文件夹到一个已有的压缩文件中
        /// </summary>
        /// <param name="fileSystem">文件或文件夹对象</param>
        /// <param name="zipStream">压缩文件流</param>
        /// <param name="root">文件或文件夹在压缩文件内的父目录</param>
        public static void AddEntry(FileSystemInfo fileSystem, ZipOutputStream zipStream, string root = "")
        {
            if (fileSystem == null || !fileSystem.Exists)
            {
                return;
            }
            if (zipStream == null)
            {
                return;
            }
            ZipEntry zipEntry = null;

            Debug.Print(string.Format("正在压缩{0}", fileSystem.Name));
            if (fileSystem.Attributes.HasFlag(FileAttributes.Directory))
            {
                zipEntry = new ZipEntry(Path.Combine(root, fileSystem.Name + "\\"));
                zipStream.PutNextEntry(zipEntry);
                OnCompress?.Invoke(fileSystem.Name, 1, 1, true);
                foreach (FileSystemInfo fileSystemInfo in (fileSystem as DirectoryInfo).GetFileSystemInfos())
                {
                    AddEntry(fileSystemInfo, zipStream, zipEntry.Name);
                }
            }
            else
            {
                zipEntry = new ZipEntry(Path.Combine(root, fileSystem.Name));
                zipStream.PutNextEntry(zipEntry);
                try
                {
                    using (FileStream readStream = new FileStream(fileSystem.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        byte[] dataBytes    = new byte[10240];
                        int    readNum      = 0;
                        uint   totalReadNum = 0;
                        while (readStream.Position < readStream.Length)
                        {
                            readNum       = readStream.Read(dataBytes, 0, dataBytes.Length);
                            totalReadNum += (uint)readNum;
                            OnCompress?.Invoke(fileSystem.Name, totalReadNum, readStream.Length, false);
                            zipStream.Write(dataBytes, 0, readNum);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }