如何制作一个zip文件

时间:2014-05-13 09:37:24

标签: c# asp.net gzip

我有一个上传按钮,当我上传文件时,我的文件必须以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";    
    }

3 个答案:

答案 0 :(得分:1)

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

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

正如下面的评论者所说,这不是“让你开始”#39;解决你确切的问题。但也许一种新方法从一开始就可以更容易。

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方法。

答案 1 :(得分:0)

版本4.5以下的.NET本身不支持ZIP文件格式。

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

答案 2 :(得分:0)

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

祝你好运..