12

I am using ICSharpCode.SharpZipLib.Zip.FastZip to zip files but I'm stuck on a problem:

When I try to zip a file with special characters in its file name, it does not work. It works when there are no special characters in the file name.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
BreakHead
  • 10,480
  • 36
  • 112
  • 165

5 Answers5

10

I think you cannot use FastZip. You need to iterate the files and add the entries yourself specifying:

entry.IsUnicodeText = true;

To tell SharpZipLib the entry is unicode.

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

// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new
    ZipOutputStream(File.Create("MyZipFile.zip")))
{
    s.SetLevel(9); // 0-9, 9 being the highest compression

    byte[] buffer = new byte[4096];

    foreach (string file in filenames)
    {
         ZipEntry entry = new ZipEntry(Path.GetFileName(file));

         entry.DateTime = DateTime.Now;
         entry.IsUnicodeText = true;
         s.PutNextEntry(entry);

         using (FileStream fs = File.OpenRead(file))
         {
             int sourceBytes;
             do
             {
                 sourceBytes = fs.Read(buffer, 0, buffer.Length);

                 s.Write(buffer, 0, sourceBytes);

             } while (sourceBytes > 0);
         }
    }
    s.Finish();
    s.Close();
 }
Paaland
  • 682
  • 1
  • 10
  • 25
  • +1000, Ey vaaaal, It is too great. It is exactly what i want. Thanks a lot. – M_Mogharrabi Jan 09 '13 at 07:17
  • @M_Mogharrabi if one of the answers has actually solved your problem you have to mark it as accepted. For instance I posted the same solution as Paaland (just scroll up), except I was faster :) – Salaros Dec 10 '16 at 20:59
  • How is Dec 20th 2012 earlier than Mar 26th 2011? And why reopen this ancient post? – Paaland Dec 11 '16 at 09:24
5

You can continue using FastZip if you would like, but you need to give it a ZipEntryFactory that creates ZipEntrys with IsUnicodeText = true.

var zfe = new ZipEntryFactory { IsUnicodeText = true };
var fz = new FastZip { EntryFactory = zfe };
fz.CreateZip("out.zip", "C:\in", true, null);
blachniet
  • 4,323
  • 5
  • 33
  • 34
1

You have to download and compile the latest version of SharpZipLib library so you can use

entry.IsUnicodeText = true;

here is your snippet (slightly modified):

FileInfo file = new FileInfo("input.ext");
using(var sw = new FileStream("output.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    using(var zipStream = new ZipOutputStream(sw))
    {
        var entry = new ZipEntry(file.Name);
        entry.IsUnicodeText = true;
        zipStream.PutNextEntry(entry);

        using (var reader = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                byte[] actual = new byte[bytesRead];
                Buffer.BlockCopy(buffer, 0, actual, 0, bytesRead);
                zipStream.Write(actual, 0, actual.Length);
            }
        }
    }
}
Salaros
  • 1,444
  • 1
  • 14
  • 34
0

Possibility 1: you are passing a filename to the regex file filter.

Possibility 2: those characters are not allowed in zip files (or at least SharpZipLib thinks so)

Joshua
  • 40,822
  • 8
  • 72
  • 132
0

try to take out the special character from the file name, i,e replace it. your Filename.Replace("&", "&");

safi
  • 3,636
  • 8
  • 24
  • 37