Andrey Vasiliykov
Andrey Vasiliykov

Reputation: 31

ZipStorer how add directory?

static string mydir = @"C:\Boba\bin\Release\ZipTest";
static string zipfile = string.Concat(mydir, ".zip");

using (ZipStorer zip = ZipStorer.Create(zipfile))
{
  zip.AddDirectory(ZipStorer.Compression.Deflate, mydir, zipfile);
}

But after I unpack the archive, folders appear

Dir: Boba -> bin > Release > ZipTest > Files...

How do I add only the ZipTest folder?


I tried to do it like this:

DirectoryInfo d = new DirectoryInfo(mydir);
zip.AddDirectory(ZipStorer.Compression.Deflate, Path.GetFileName(d.FullName), Path.GetFileName(d.FullName), comment);

A Zip archive is created, and inside a folder called ZipTestZipTest, inside there are files and an empty archive called .zip.
How to make it just ZipTest inside the archive?
And so that there is no empty archive in the ZipTest folder?

Upvotes: 2

Views: 249

Answers (2)

r3xq1
r3xq1

Reputation: 26

If you need without an internal directory, then you can do this:

public static void PackToZipWithoutInternalDir(string dir, string zipout, string comment = "") 
{
    if (Directory.Exists(dir) && !string.IsNullOrWhiteSpace(dir) && !string.IsNullOrWhiteSpace(zipout))
    {
        try
        {
           using var zip = ZipStorer.Create(zipout, comment); // true for stream
           zip.EncodeUTF8 = true; // Text encoding
           zip.ForceDeflating = true; // Force file compression
           
           foreach (string listDir in Directory.EnumerateDirectories(dir, "*", SearchOption.TopDirectoryOnly))
           {
              // Add folders with files to the archive
              zip.AddDirectory(ZipStorer.Compression.Deflate, listDir, string.Empty);
           }
          
           foreach (string listFiles in Directory.EnumerateFiles(dir, "*.*", SearchOption.TopDirectoryOnly))
           {
               // Add residual files in the current directory to the archive.
               zip.AddFile(ZipStorer.Compression.Deflate, listFiles, Path.GetFileName(listFiles));
           }
        }
        catch (Exception ex) { Console.WriteLine(ex); }
    }
}

Use:

namespace ZipStorerEx
{
    using System;
    using System.IO;

    public static class Program
    {
        private static readonly string CurrDir = Environment.CurrentDirectory;
        private static readonly string BeginDir = Path.Combine(CurrDir, "YouDir");
        private static readonly string ZipOut = $"{BeginDir}.zip";

        [STAThread]
        public static void Main()
        {
            Console.Title = "ZipStorerEx";

            PackToZipWithoutInternalDir(BeginDir, ZipOut, "It's Good");
            Console.ReadKey();
        }
    }
}

Upvotes: 1

Auditive
Auditive

Reputation: 2049

Try set 3rd argument (_pathnameInZip) for .AddDirectory as empty string:

string dir = @"C:\Boba\bin\Release\ZipTest";
string zipFile = string.Concat(dir, ".zip");
string comment = "My ZipTest";

using (ZipStorer zip = ZipStorer.Create(zipFile))
{
    zip.AddDirectory(ZipStorer.Compression.Deflate, dir, string.Empty, comment);
}

Upvotes: 2

Related Questions