C# 你知道为什么在使用归档实用程序时文件夹被提取为文件名吗?
我正在使用ICSharpCode.SharpZipLib.Zip打包文件,以便我的客户下载他们的文件。 但是,在Mac上使用Archive Utility时,提取的文件都位于同一文件夹中,原始文件路径为文件夹名。但是如果使用其他程序解压没有问题。 有什么想法吗 代码:C# 你知道为什么在使用归档实用程序时文件夹被提取为文件名吗?,c#,asp.net,C#,Asp.net,我正在使用ICSharpCode.SharpZipLib.Zip打包文件,以便我的客户下载他们的文件。 但是,在Mac上使用Archive Utility时,提取的文件都位于同一文件夹中,原始文件路径为文件夹名。但是如果使用其他程序解压没有问题。 有什么想法吗 代码: 将条目名称中的向后斜杠替换为向前斜杠 请勾选此问题,例如: 将条目名称中的向后斜杠替换为向前斜杠 请勾选此问题,例如: 不起作用,还是一样的问题。问题是如果我使用其他程序解压文件夹,文件夹结构是正确的。不工作,仍然是相同的问题
将条目名称中的向后斜杠替换为向前斜杠 请勾选此问题,例如:
将条目名称中的向后斜杠替换为向前斜杠 请勾选此问题,例如:
不起作用,还是一样的问题。问题是如果我使用其他程序解压文件夹,文件夹结构是正确的。不工作,仍然是相同的问题。问题是,如果我使用其他程序解压文件夹,文件夹结构是正确的。
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();
}
}
}
}
....
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
{...}