1

I'm using ICSharpCode.SharpZipLib.Zip to pack files for my clients to download their files. However when using Archive Utility on Mac, the extracted files are all in a same folder with the original file path as folder name. But if using other program to unzip with no problems. Any ideas?

enter image description here enter image description here

Code:

using System;
using System.IO;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

using System.Runtime.InteropServices;

public class ZipClass
{

    /// <summary>
    /// Check file writing status
    /// </summary>
    /// <param name="lpPathName"></param>
    /// <param name="iReadWrite"></param>
    /// <returns></returns>
    [DllImport("kernel32.dll")]
    public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
    [DllImport("kernel32.dll")]
    public static extern bool CloseHandle(IntPtr hObject);
    public const int OF_READWRITE = 2;
    public const int OF_SHARE_DENY_NONE = 0x40;
    public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
    public bool FileOccupyStatus(string vFileName)
    {
        IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
        if (vHandle == HFILE_ERROR)
        {
            //MessageBox.Show("File being used");
            return false;
        }
        CloseHandle(vHandle);
        //MessageBox.Show("File free");
        return true;
    }

    ZipOutputStream zos = null;
    string strBaseDir = "";
    public bool ZipFolder(string folder, string zipFile)
    {
        zos = new ZipOutputStream(File.Create(zipFile));
        zos.Password = "";
        strBaseDir = folder + "\\";
        addZipEntry(strBaseDir);
        //ZipFileDictory(strBaseDir, zos, "");
        zos.Finish();
        zos.Close();
        return true;
    }

    void addZipEntry(string PathStr)
    {
        DirectoryInfo dir = new DirectoryInfo(PathStr);
        foreach (DirectoryInfo folder in dir.GetDirectories())
        {
            addZipEntry(folder.FullName + "\\");
        }
        foreach (FileInfo item in dir.GetFiles())
        {
            if (FileOccupyStatus(item.FullName.ToString()))
            {
                if (item.Extension.ToLower() == ".pdf")
                {   
                    continue;
                }
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(strBaseDir, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                entry.IsUnicodeText = true;
                entry.Size = fs.Length;
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
    }
}

Call function from webpage:

....
string folder = dr["folder"].ToString().Replace("/", "\\");
ZipClass zip = new ZipClass();

folder = Server.MapPath("/data") + "\\" + folder;
try
{
    zip.ZipFolder(folder, folder + "\\media.zip");
    Response.Write("{\"status\":\"true\",\"file\":\"/" + dr["zip"] + "\"}");
}
catch
{...}
Waq
  • 29
  • 9

1 Answers1

0

Replace backwards slashes with forward slashes in the entry name.

Check this question for example: How do you add a folder to a zip archive with ICSharpCode.SharpZipLib

Community
  • 1
  • 1
Alex
  • 14,338
  • 5
  • 41
  • 59
  • Not working, still the same issue. The problem is if I use other program to unzip the folder, the folder structure is correct. – Waq Jan 08 '16 at 07:02