SharpLibZip: Add file without path
Asked Answered
B

3

16

I'm using the following code, using the SharpZipLib library, to add files to a .zip file, but each file is being stored with its full path. I need to only store the file, in the 'root' of the .zip file.

string[] files = Directory.GetFiles(folderPath);
using (ZipFile zipFile = ZipFile.Create(zipFilePath))
{
     zipFile.BeginUpdate();
     foreach (string file in files)
     {
          zipFile.Add(file);
     }
     zipFile.CommitUpdate();
}

I can't find anything about an option for this in the supplied documentation. As this is a very popular library, I hope someone reading this may know something.

Birchard answered 13/10, 2008 at 17:4 Comment(0)
B
25

My solution was to set the NameTransform object property of the ZipFile to a ZipNameTransform with its TrimPrefix set to the directory of the file. This causes the directory part of the entry names, which are full file paths, to be removed.

public static void ZipFolderContents(string folderPath, string zipFilePath)
{
    string[] files = Directory.GetFiles(folderPath);
    using (ZipFile zipFile = ZipFile.Create(zipFilePath))
    {
        zipFile.NameTransform = new ZipNameTransform(folderPath);
        foreach (string file in files)
        {
            zipFile.BeginUpdate();
            zipFile.Add(file);
            zipFile.CommitUpdate();
        }
    }
}

What's cool is the the NameTransform property is of type INameTransform, allowing customisation of the name transforms.

Birchard answered 14/10, 2008 at 12:11 Comment(1)
Thanks a lot. After so much struggle I could solved my issue.Patrica
E
12

How about using System.IO.Path.GetFileName() combined with the entryName parameter of ZipFile.Add()?

string[] files = Directory.GetFiles(folderPath);
using (ZipFile zipFile = ZipFile.Create(zipFilePath))
{
     zipFile.BeginUpdate();
     foreach (string file in files)
     {
          zipFile.Add(file, System.IO.Path.GetFileName(file));
     }
     zipFile.CommitUpdate();
}
Elana answered 13/10, 2008 at 17:12 Comment(4)
There is no overload with an entryName parameter for ZipFile.Add() when adding a file by name, but I like your thinking. See my answer below.Birchard
Do you have the latest version of the library? It does have an overload on the file.addCotemporary
I am pretty sure there is, at least in the latest version.Elana
Had the same problem myself today, updated my library to the latest version, used to overload for Add and it worked a treat.Belindabelisarius
A
4

The MSDN entry for Directory.GetFiles() states that The returned file names are appended to the supplied path parameter. (http://msdn.microsoft.com/en-us/library/07wt70x2.aspx), so the strings you are passing to zipFile.Add() contain the path.

According to the SharpZipLib documentation, there is an overload of the Add method,

public void Add(string fileName, string entryName) 
Parameters:
  fileName(String) The name of the file to add.
  entryName (String) The name to use for the ZipEntry on the Zip file created.

Try this approach:

string[] files = Directory.GetFiles(folderPath);
using (ZipFile zipFile = ZipFile.Create(zipFilePath))
{
     zipFile.BeginUpdate();
     foreach (string file in files)
     {
          zipFile.Add(file, Path.GetFileName(file));
     }
     zipFile.CommitUpdate();
}
Ammann answered 13/10, 2008 at 17:12 Comment(1)
Yes, if you exclude the directory from the file path you are adding, the Add method won't find the file. See my answer below.Birchard

© 2022 - 2024 — McMap. All rights reserved.