使用DotNetZip库从内存流创建zip文件时如何传递文件夹层次结构

要求:1)在多个段中创建分割的zip文件(例如大小-1 GB / 500 MB),以便可以通过浏览器下载它们。所有段的总zip量可能超过10 GB 2)zip内容可以是多个文件或包含子文件夹和文件的文件夹3)以流的形式从Cloud读取文件的内容。文件的元信息(如文件夹层次结构)在本地可用

我正在使用DotNetZip库来完成任务。代码如下:

long length = default(long);
            Stream fileReadStream;

            long Space = default(long);
            string tempZipFile = string.Empty;
            FileZipStatus oldStatue = new FileZipStatus();
            byte[] Buffer = new byte[1024 * 1024];

            if (zipFileName != null && !zipFileName.ToUpper().EndsWith(".ZIP")) zipFileName += ".zip";
            string strTempFolder = "";
            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
            {
                try
                {
                    strTempFolderPath = tempZipOutPutFilePath + "\\";
                    string strTempFolderName = DateTime.Now.Ticks.ToString();
                    strTempFolder = strTempFolderPath + strTempFolderName;
                    if (userFileList.Count > 0)
                    {
                        if (Directory.Exists(strTempFolder))
                        {
                            Directory.Delete(strTempFolder);
                        }
                        Directory.CreateDirectory(strTempFolder);
                    }
                    foreach (UserFile userFile in userFileList)
                    {
                        WebResponse response = null;
                        try
                        {
                            WebRequest request = null;
                            IDictionary<string, object> _dictionary = new Dictionary<string, object>();
                            /// First 
                            FileSystemEnum fileSysEnum = FileSystemBase.GetFileSystemEnumByStorageId(userFile.StorageId);
                            IFileSystemLib ifileSystemLocal = FileSystemFactory.GetSpecificInstance(fileSysEnum);

                            fileReadStream = ifileSystemLocal.GetFile(userFile.FilePath, userFile.GuidName, ref request, ref response, _dictionary);
                            long filesize = default(long);

                            long.TryParse(ifileSystemLocal.GetFileContentLength(userFile.FilePath, userFile.GuidName).ToString(), out filesize);
                            Space = (Space > default(long)) ? (Space + filesize) : filesize;
                            //Now we have to store the data, so that we must access the file

                            int dataToRead;
                            FileStream writeStream = new FileStream(strTempFolder + "\\" + userFile.FileName, FileMode.Create, FileAccess.Write);
                            while ((dataToRead = fileReadStream.Read(Buffer, 0, Buffer.Length)) > 0)
                            {
                                writeStream.Write(Buffer, 0, dataToRead);
                            }
                            writeStream.Close();

                            zip.AddFile(strTempFolder + "\\" + userFile.FileName, userFile.RelativePath);
                            fileReadStream.Close();                            
                        }
                        catch (Exception ex)
                        {
                            LogManager.Trace(ex, "ZIpping Block - ZIPFileName", zipFileName + "File to zip" + userFile.GuidName);
                        }
                        finally
                        {
                            if (response != null) response.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    _currentStatus = FileZipStatus.NotAvailable;
                    oldStatue = UpdateZipStatus(ObjectZipID, Space, FileZipStatus.Failed);
                    throw ex;
                }
                finally
                {   
                }
                try
                {
                    zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
                    zip.MaxOutputSegmentSize = 200 * 1024 * 1024;   // 200 mb
                    zip.Save(strTempFolderPath + "\\" + zipFileName);
                    oldStatue = UpdateZipStatus(ObjectZipID, Space, FileZipStatus.Available);
                    length = new FileInfo(strTempFolderPath + "\\" + zipFileName).Length;
                    _currentStatus = FileZipStatus.Available;
                    // deleting temp folder
                    Directory.Delete(strTempFolder, true);
                }
                catch (Exception ex)
                {
                    _currentStatus = FileZipStatus.NotAvailable;
                    oldStatue = UpdateZipStatus(ObjectZipID, Space, FileZipStatus.Failed);
                    length = default(long);
                    throw ex;
                }
            }

上面的代码中使用的DotNetZip libray有一个限制它要么需要a)作为输入保存在磁盘上的文件。在这种情况下,可以为每个文件传递文件夹层次结构信息。或2)如果将流作为输入传递,则无法为文件传递文件夹层次结构信息。

我需要传递每个文件的文件夹层次结构信息,并从流中读取输入。由于zip内容可能很大(可能超过10 GB),因此不想将文件保存在Web服务器的临时存储中。创建zip文件时,有人可以帮忙喜欢如何传递文件夹层次结构吗?谢谢

我得到了解决方案。这是代码

private void button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConn = new SqlConnection(@"Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;Integrated Security=True"))
            {
                string query = String.Format(@"SELECT [FilePath],[FileName],[FileData] FROM [TestTable]");
                SqlCommand cmd = new SqlCommand(query, sqlConn);
                cmd.Connection.Open();

                System.IO.MemoryStream memStream = null;
                ZipFile zip = new ZipFile();
                zip.MaxOutputSegmentSize = 1024 * 1024; // 1MB each segment size would be
                // the above line would split zip file into multiple files and each file
                //size would be 1MB
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        byte[] data = (byte[])reader["FileData"];
                        memStream = new System.IO.MemoryStream(data);
                        string strFile = reader["FilePath"].ToString() + "\\" + reader["FileName"].ToString();
                        ZipEntry ze = zip.AddEntry(strFile, memStream);
                    }
                }
                zip.Save(@"e:\MyCustomZip.zip");
                memStream.Dispose();
                MessageBox.Show("Job Done");
                // here u can save the zip in memory stream also there is a overload insteaa of saving in HD
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Finder在嵌套文件夹层次结构中创建新文件夹?

Py:使用嵌套文件夹创建文件夹层次结构

如何批量更改文件的文件夹层次结构?

如何从文件夹层次结构创建平面目录并处理重复文件?

在PowerShell中使用New-RsFolder递归创建文件夹层次结构

重复复制特定模式的文件并重新创建文件夹层次结构

如何复制某些文件(不带文件夹层次结构),但不覆盖现有文件?

Java 多文件和文件夹层次结构

如何从Jupyter Notebook下载所有文件和文件夹层次结构?

GoogleDrive API - 如何从python中的文件获取文件夹层次结构

如何在文件夹层次结构中找到所有不同的贪婪文件后缀?

如何在文件夹层次结构中找到所有不同的文件扩展名?

build.gradle中的任务创建文件夹层次结构

在Java中通过FTP创建文件夹层次结构

Visual Studio构建文件夹层次结构

如何在大文件夹层次结构中进行文本替换?

我如何git“ subtree split”但保持完整的文件夹层次结构?

使用Google Drive API [C#/ .NET]获取文件夹层次结构

使用Python从MySQL重建JSON格式的文件夹层次结构

如何在文件夹层次结构中引用作业?(Jenkins Cloudbees文件夹插件)

在FileNet中搜索特定文件夹的文件夹层次结构

无法在“文件夹层次结构栏”中键入文件夹路径

当传入内存流时,DotNetZip创建0kb文件

如何在解决方案资源管理器上保持源文件夹层次结构?

如何在OneNote 2013中显示文件夹层次结构中的当前部分

如何使用DotNetZip读取zip文件中的文件夹中的单个文件?

在受限外壳中列出文件和文件夹层次结构

在文件夹层次结构中搜索重复的文件名?

反应上下文文件夹层次结构/体系结构?