0

File can add with full path easily like :

 z.Add(fpaths);

But i wants to add only file not full path use code below but a error :

 z.Add(fpaths, filenamed); error is! some invalid arguments 

Please have a look my code and let me know how can i manage this code

protected void btnAcceptAll_Click(object sender, EventArgs e)
 {
    int count = 0;
    string msg = string.Empty;
    string filenamezip = "FileFolder\\FilesZip.zip";
    string strPathAndQuery = HttpContext.Current.Request.PhysicalApplicationPath;
    string paths = strPathAndQuery + filenamezip;
    ZipFile z = ZipFile.Create(paths);
    z.BeginUpdate();
    foreach (GridViewRow gvrow in gv.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
        if (chk != null & chk.Checked)
        {
            count++;
            string dirPath = gv.DataKeys[gvrow.RowIndex].Values[3].ToString();
            string fileName = gv.DataKeys[gvrow.RowIndex].Values[1].ToString();
            string filePath = dirPath + "/" + fileName;
            string fpaths = strPathAndQuery + filePath;
            string filenamed = Path.GetFileName(fpaths);
            z.Add(fpaths, filenamed);  //show error
        }
    }
    if (count > 0)
    {
        z.CommitUpdate();
        z.Close();
        BindGrid();
        Session["filenamezip"] = filenamezip;
        ClientScript.RegisterStartupScript(GetType(), "SomeNameForThisScript", "window.open('DownloadZip.aspx', 'DownloadWindow','width=400,height=200');", true);
    }

}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

1 Answers1

0

Try using Path.Combine() instead of concatenation for fpaths:

string fpaths = Path.Combine(strPathAndQuery, filePath);

Also, make sure that when you pass fpaths or filenamed to .Add(string, string), neither one is null.

Here's the source of the method you're trying to use:

/// <summary>
/// Add a file to the archive.
/// </summary>
/// <param name="fileName">The name of the file to add.</param>
/// <param name="entryName">The name to use for the <see cref="ZipEntry"/> on the Zip file created.</param>
/// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
public void Add(string fileName, string entryName)
{
    if (fileName == null) 
    {
        throw new ArgumentNullException("fileName");
    }

    if ( entryName == null ) 
    {
        throw new ArgumentNullException("entryName");
    }

    CheckUpdating();
    AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName, entryName, true)));
}

Since you're able to Add using .Add(string), I suspect that you're passing a null to the overload you're trying to use.

B.K.
  • 9,982
  • 10
  • 73
  • 105