1

I am generating a zip file from the folder D:\Nagaraj\Dotnet\Zipfile\Zipfile\Filebuild\Hi. There are 2 txt files within the folder.

But problem is that within the zip file there is the path D:\Nagaraj\Dotnet\Zipfile\Zipfile\Filebuild\Hi and within that folder are the 2 txt files.

Now I need to remove the path D:\Nagaraj\Dotnet\Zipfile\Zipfile\Filebuild\Hi and directly generate the Hi.zip with the 2 txt file in the root of the archive. I'm using SharpZipLib for creating the archive.

protected void Page_Load(object sender, EventArgs e)
{
    StartZip("D:/Nagaraj/Dotnet/Zipfile/Zipfile/Filebuild/Hi",".zip");        
}

public void StartZip(string directory, string zipFileName)
{
    ZipFile z = ZipFile.Create(directory + zipFileName);
    z.BeginUpdate();
    string[] filenames = Directory.GetFiles(directory);
    foreach (string filename in filenames)
    {
        z.Add(filename);
    }
    z.CommitUpdate();
    z.Close();
}
ssube
  • 47,010
  • 7
  • 103
  • 140
user1225988
  • 41
  • 2
  • 6

1 Answers1

2

The problem looks to be how your creating the zip file. Your passing in the directory where the files are located in your call to Add.

Instead, just pass the file names using the Path.GetFileName method:

public void StartZip(string directory, string zipFileName)
{
    ZipFile z = ZipFile.Create(directory + zipFileName);
    z.BeginUpdate();

    string[] filenames = Directory.GetFiles(directory);

    foreach (string filename in filenames)
    {
        z.Add(Path.GetFileName(fileName));
    }
    z.CommitUpdate();
    z.Close();
}
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • I am used the above coding ..the problem is..I can't find out where the zip file in created or whether created or not – user1225988 Feb 24 '12 at 13:30
  • You'll have to forgive me, but I don't think I understand your comment. Do you mean that after creating the zip file, you do not know where that zip has been created on the disk? – Jason Evans Feb 24 '12 at 13:56
  • ya.....I find out which path it's created .This is created in this path "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE" ....but problem is .I am trying to open that file .....it will show error "damaged data" – user1225988 Feb 24 '12 at 14:23
  • I'm not familiar with `Sharpziplib` but I would recommend looking at their documentation to see if there is a configuration option for the folder where the zip file will be created. – Jason Evans Feb 24 '12 at 14:34