0
votes

I am trying to use the ZipFiles() utility method and its producing an empty zip file. I am using Sitecore 6.5. There are no error, permissions or otherwise.

Any thoughts? Here is the code.

public void CreateZipFile(string zipfileName, List<string> files)
{
var zipfile = string.Format("{0}/{1}/{2}", TempFolder.Folder, "myfolder", zipfileName) ;
var fileArray = files.ToArray();
var x = FileUtil.ZipFiles(zipfile, fileArray);
}

EDIT: I am passing the files like this

var files = new List<string> { FileUtil.MapPath("/temp/sample.xlf") };
1
In your files list do you pass the absolute path of the file (e.g. c:\inetpub\readme.text) or just relative to the web app root? - Marek Musielak
I have tried both, currently I am passing var files = new List<string> { FileUtil.MapPath("/temp/sample.xlf") }; - mluker
The proper usage is FileUtil.ZipFiles("/test.zip", new []{"/web.config", "/otherfile.txt"}) and the file will be created in the root of your web app. - Marek Musielak
Using your suggestion, I have the same result. An empty 45kb zip file. - mluker
45kB is a big size for an empty zip. Are you sure that there are no hidden files or directories in the zip? - Marek Musielak

1 Answers

3
votes

The proper usage of FileUtil.ZipFiles method is:

FileUtil.ZipFiles("/test.zip", new []{"/web.config", "/otherfile.txt"})

Sitecore automatically maps paths. The zip file will be created in your web app root.


EDIT AFTER COMMENT

If you want to create a zip file outside the web root and with a flat structure inside, you can use Sitecore ZipWriter class like this:

public static string ZipFiles(string absolutePathToZipfile, string[] files)
{
    using (ZipWriter zipWriter = new ZipWriter(absolutePathToZipfile))
    {
        foreach (string path in files)
        {
            using (FileStream fileStream = System.IO.File.OpenRead(path.StartsWith("/") ? FileUtil.MapPath(path) : path))
                zipWriter.AddEntry(FileUtil.GetFileName(path), fileStream);
        }
    }
    return absolutePathToZipfile;
}