【问题标题】:how to make a zip file [closed]如何制作一个zip文件[关闭]
【发布时间】:2014-05-13 09:37:24
【问题描述】:

我有一个上传按钮,当我上传文件时,我的文件必须以 zip 或压缩形式上传到服务器上的指定路径

我试过 ===>

        string strFileName = string.Empty;
        string strserverPath = string.Empty;
        if (UploadFiles.HasFile)
        {
            string abcPATH = tvFolders.SelectedValue.ToString();
            string rootPath = tvFolders.SelectedNode.ToString();
            string fname = Path.GetFileName(UploadFiles.PostedFile.FileName);
            try
            {
                strserverPath = abcPATH + "\\" + fname;

                //string strName = Path.GetFileName(UploadFiles.PostedFile.FileName);

                Stream myStream = UploadFiles.PostedFile.InputStream;
                byte[] myBuffer = new byte[myStream.Length + 1];
                myStream.Read(myBuffer, 0, myBuffer.Length);
                myStream.Close();
                FileStream myCompressedFile = default(FileStream);
                myCompressedFile = File.Create(Server.MapPath(Path.ChangeExtension("~/"+strserverPath, "zip")));
                GZipStream myStreamZip = new GZipStream(myCompressedFile, CompressionMode.Compress);
                myStreamZip.Write(myBuffer, 0, myBuffer.Length);
                myStreamZip.Close();
                //asp.net c#
                //UploadFiles.PostedFile.SaveAs(Server.MapPath("~/" + strserverPath));
                listofuploadedfiles.Text = "done";
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
        }
        else
            listofuploadedfiles.Text = "not done";    
    }

【问题讨论】:

  • 您看到的问题/错误信息是什么?
  • 问题是什么?您是否尝试上传 zip 文件但无法上传?
  • 是的先生,我在我的代码中使用了树视图,我想在选定的节点中以 zip 格式上传我的文件。

标签: c# asp.net gzip


【解决方案1】:

在 .net 4.5 中有一个新的 System.IO.Compression.ZipFile 命名空间,它有一个友好的 api 来创建 zip 文件,而不是使用棘手的 .net 3.0 包方法。

唯一需要注意的是它适用于文件夹结构而不是直接使用文件。

正如下面的评论者所建议的那样,这不是解决您确切问题的“入门”。但也许是一种从一开始就更容易的新方法。

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

取自http://msdn.microsoft.com/en-us/library/vstudio/system.io.compression.zipfile

它应该可以让您开始使用 zipfile 方法。

【讨论】:

  • OP 显示的代码不工作,不要求开始。请使用评论要求澄清,而不是发布通用的重复答案,或相应地重复投票。
  • 好点@CodeCaster 我已经添加了一些细节,它是一种新方法。
  • 先生,我想要动态路径,而我的文件不是 zip 文件,实际上我在上传我的 exp 文件时想要。我现在有 xyz.pdf 文件,当我上传到服务器时,我的文件必须以 zip/rar 文件的形式上传,例如 xyz.zip/rar
  • @user3622698 你可以先为每个上传的文件创建一个文件夹msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx
【解决方案2】:

.NET 4.5 以下不原生支持 ZIP 文件格式。

您可以查看添加该支持的库,例如 SharpZipLib

【讨论】:

【解决方案3】:
using System;
using System.IO;
using System.IO.Packaging;

namespace ZipSample
{
class Program
{
    static void Main(string[] args)
    {
        AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
        AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe");
    }

    private const long BUFFER_SIZE = 4096;

    private static void AddFileToZip(string zipFilename, string fileToAdd)
    {
        using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
        {
            string destFilename = ".\\" + Path.GetFileName(fileToAdd);
            Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
            if (zip.PartExists(uri))
            {
                zip.DeletePart(uri);
            }
            PackagePart part = zip.CreatePart(uri, "",CompressionOption.Normal);
            using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
            {
                using (Stream dest = part.GetStream())
                {
                    CopyStream(fileStream, dest);
                }
            }
        }
    }

    private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
    {
        long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = 0;
        long bytesWritten = 0;
        while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
            bytesWritten += bufferSize;
        }
    }
}
}

已通过这些网站参考编码

1.Creating Zip archives in .NET (without an external library like SharpZipLib) 2.http://madprops.org/blog/zip-your-streams-with-system-io-packaging/
3.Create normal zip file programmatically

其他参考:

您可以参考这些网站。它可能对您有用。

http://forums.asp.net/t/1086292.aspx?How+to+create+zip+file+using+C+

Create normal zip file programmatically

http://www.dotnetperls.com/zipfile

http://www.aspdotnet-suresh.com/2013/04/create-zip-files-in-aspnet-c-vbnet-zip.html

http://www.codeproject.com/Questions/450505/create-zip-file-in-asp-net

祝你好运..

【讨论】:

  • 我看到了你的最后一个链接。在那个“使用 Ionic.Zip”的用途以及我如何使用这个文件 bcoz 我在询问 qsion 之前尝试过你的链接代码,并且我遇到了那个 .dll 文件的问题,所以请帮助我了解那个 Ionic。 Zip.dll 文件。
猜你喜欢
  • 2022-01-04
  • 1970-01-01
  • 2014-11-02
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2011-06-16
  • 2021-09-18
相关资源
最近更新 更多