ICSharpCode.SharpZipLib.Zip.FastZip not zipping files having special characters in their file names
Asked Answered
T

5

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.

Timotheus answered 22/11, 2010 at 13:6 Comment(7)
When you say it doesn't work, what doesn't work? Can you please post your code and the error message you are getting?Henrietta
It not zip that file, also I am not getting any errorTimotheus
does it work without the special characters?Christiechristin
What special character? Can you give an example?Hen
HI-ASCII characters example €...I dnt think you guys dnt know what means special characters :(Timotheus
@BreakHead: that's not a special character to me. It's a non-ASCII character.Gamb
It's a feature, not a bug.Dietitian
G
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();
 }
Giliane answered 26/3, 2011 at 8:55 Comment(3)
+1000, Ey vaaaal, It is too great. It is exactly what i want. Thanks a lot.Bellebelleek
@Bellebelleek 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 :)Columbic
How is Dec 20th 2012 earlier than Mar 26th 2011? And why reopen this ancient post?Giliane
N
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);
Nyasaland answered 27/8, 2015 at 18:32 Comment(1)
This is a fully working solution, no need to hack around with manual zip entries :)Hume
C
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);
            }
        }
    }
}
Columbic answered 20/12, 2012 at 9:17 Comment(0)
F
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)

Fillin answered 22/11, 2010 at 19:22 Comment(0)
S
0

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

Straightforward answered 27/4, 2011 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.