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?
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
{...}