绝对路径目录是在使用 ASP.NET 的 zip 文件下载应用程序期间创建的

本文关键字:zip NET 文件下载 应用程序 创建 ASP 路径 | 更新日期: 2023-09-27 18:35:31

使用它,我编写了代码,首先创建一个zip文件夹,然后在用户单击全部下载按钮时下载zip文件。它工作正常。但是当用户提取该zip文件H:/abd/zyc/questionpapers/papername/questionpaper.pdf(绝对路径)时。但我想要的是当用户提取zip文件时,用户必须只获得papername文件夹而不是其他文件夹。请帮助我如何解决这个问题。

这是我的代码。

protected void Page_Load(object sender, EventArgs e)
{
    StartZip(Request.QueryString["path"].ToString(), Request.QueryString["fname"].ToString());
    fileDownload(Request.QueryString["fname"].ToString(), Server.MapPath(Request.QueryString["fname"].ToString()));
}

public void StartZip(string directory, string zipfile_path)
    {
        // the directory you need to zip 
        string[] filenames = Directory.GetFiles(directory);
        // path which the zip file built in 
        ZipOutputStream s = new ZipOutputStream(File.Create(zipfile_path));
        foreach (string filename in filenames)
        {
            FileStream fs = File.OpenRead(filename);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(filename);
            s.PutNextEntry(entry);
            s.Write(buffer, 0, buffer.Length);
            fs.Close();

        }
        s.SetLevel(5);
        s.Finish();
        s.Close();
    }
    private void fileDownload(string fileName, string fileUrl)
    {
        Page.Response.Clear();
        bool success = ResponseFile(Page.Request, Page.Response, fileName, fileUrl, 1024000);
        if (!success)
            Response.Write("Downloading Error!");
        Page.Response.End();
    }
    public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
    {
        try
        {
            FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader br = new BinaryReader(myFile);
            try
            {
                _Response.AddHeader("Accept-Ranges", "bytes");
                _Response.Buffer = false;
                long fileLength = myFile.Length;
                long startBytes = 0;
                int pack = 10240; //10K bytes
                int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
                if (_Request.Headers["Range"] != null)
                {
                    _Response.StatusCode = 206;
                    string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                    startBytes = Convert.ToInt64(range[1]);
                }
                _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                if (startBytes != 0)
                {
                    _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                }
                _Response.AddHeader("Connection", "Keep-Alive");
                _Response.ContentType = "application/octet-stream";
                _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
                br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
                for (int i = 0; i < maxCount; i++)
                {
                    if (_Response.IsClientConnected)
                    {
                        _Response.BinaryWrite(br.ReadBytes(pack));
                        Thread.Sleep(sleep);
                    }
                    else
                    {
                        i = maxCount;
                    }
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                br.Close();
                myFile.Close();
            }
        }
        catch
        {
            return false;
        }
        return true;
    }

绝对路径目录是在使用 ASP.NET 的 zip 文件下载应用程序期间创建的

这应该有效...

ZipEntry entry = new ZipEntry(Path.GetFileName(filename)); // just the file name