-1

我在服务器上有一个名为“用户”的文件夹。

有很多用户在使用这个网站。

我为每个用户创建一个不同的文件夹。文件夹的名称将是用户的名称。

在那我有一些默认的文本文件和一个名为“上传”的文件夹

任何用户上传的文件都将存储在“已上传”文件夹中。

因此,对于任何特定用户,他的文件将位于“用户/用户名/上传”中。

现在我想从服务器备份我电脑上的这些庞大数据。所以我想下载名为“用户”的文件夹。

我的网站托管在 some.com 上。他们没有提供在我的计算机上下载该数据的便利。

所以我决定为我创建一个下载页面。

现在的问题是如何下载这个名为“Users”的文件夹?或者如何将此文件夹转换为 zip?

4

1 回答 1

1

首先,我从这个链接下载了一个 zip 文件解压缩 并在第三个文件夹中添加了对 dll 的引用。

使用部分:

Using System.IO;
Using ICSharp.SharpZipLib.Zip;

代码 :

ZipOutputStream zos;
String strBaseDir;

protected void Page_Load(object sender, EventArgs e)
    {
        StartZip(Server.MapPath("directory name"), "filename");
    }


    protected void StartZip(string strPath, string strFileName)
    {
        MemoryStream ms = null;
        Response.ContentType = "application/octet-stream";
        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
        ms = new MemoryStream();
        zos = new ZipOutputStream(ms);
        strBaseDir = strPath + "\\";
        addZipEntry(strBaseDir);
        zos.Finish();
        zos.Close();
        Response.Clear();
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }


    protected void addZipEntry(string PathStr)
    {
        DirectoryInfo di = new DirectoryInfo(PathStr);
        foreach (DirectoryInfo item in di.GetDirectories())
        {
            addZipEntry(item.FullName);
        }
        foreach (FileInfo item in di.GetFiles())
        {
            FileStream fs = File.OpenRead(item.FullName);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            string strEntryName = item.FullName.Replace(strBaseDir, "");
            ZipEntry entry = new ZipEntry(strEntryName);
            zos.PutNextEntry(entry);
            zos.Write(buffer, 0, buffer.Length);
            fs.Close();
        }
    }

我从这个链接得到这个代码

我将其转换为 vb.net。下面是 VB.NET 用户的代码:

进口部分:

Imports System.IO
Imports ICSharp.SharpZipLib.Zip

代码 :

Dim zos as ZipOutputStream
Dim strBaseDir as String

Public Sub btnBackUpDatabase_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackUpDatabase.Click

        StartZip(Server.MapPath("~/App_Data"), "Database")

    End Sub

    Protected Sub StartZip(ByVal strPath As String, ByVal strFileName As String)
        Dim ms As IO.MemoryStream = Nothing
        Response.ContentType = "application/octet-stream"
        strFileName = HttpUtility.UrlEncode(strFileName).Replace("+"c, " "c)
        Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName & ".zip")
        ms = New IO.MemoryStream()
        zos = New ZipOutputStream(ms)
        strBaseDir = strPath & "\"
        addZipEntry(strBaseDir)
        zos.Finish()
        zos.Close()
        Response.Clear()
        Response.BinaryWrite(ms.ToArray())
        Response.[End]()

    End Sub


    Protected Sub addZipEntry(ByVal PathStr As String)
        Dim di As New IO.DirectoryInfo(PathStr)
        For Each item As IO.DirectoryInfo In di.GetDirectories()
            addZipEntry(item.FullName)
        Next
        For Each item As IO.FileInfo In di.GetFiles()
            Dim fs As IO.FileStream = IO.File.OpenRead(item.FullName)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            Dim strEntryName As String = item.FullName.Replace(strBaseDir, "")
            Dim entry As New ZipEntry(strEntryName)
            zos.PutNextEntry(entry)
            zos.Write(buffer, 0, buffer.Length)
            fs.Close()
        Next
    End Sub
于 2013-05-14T22:45:07.107 回答