我正在使用图书馆ICSharpCode.SharpZipLib.Zip
;
我的代码如下:
\\ALAWP\\THIS\\ACORD\\
ZIPDirectory
但是当它完成时,文件名不是acord_combined.txt
,而是名为ACORD\acord_combined.txt
我做错了什么?
public void CleanRoot()
{
DirectoryInfo RootDi = new DirectoryInfo(FilePrep.RootDirectory);
string ZipDirectory = FilePrep.RootDirectory + "\\processed\\AceKey"+ DateTime.Now.ToString("yyyyMMdd_H;mm;ss") +".zip";
ZipOutputStream NewZipOutput = new ZipOutputStream(File.Create(ZipDirectory));
foreach (FileInfo fi in RootDi.GetFiles("acord*.*"))
{
Compress(ref NewZipOutput, fi);
//MoveFile(fi.FullName,ZipDirectory);
}
NewZipOutput.Finish();
NewZipOutput.Close();
}
public void Compress(ref ZipOutputStream ZipFolder, FileInfo fi)
{
try
{
FileStream fsFileToBeZipped = fi.OpenRead();
ZipEntry entry = new ZipEntry(fi.FullName);
ZipFolder.PutNextEntry(entry);
int size = 2048;
byte[] buffer = new byte[size];
while (true)
{
size = fsFileToBeZipped.Read(buffer, 0, buffer.Length);
if (size > 0)
ZipFolder.Write(buffer, 0, size);
else
break;
} //end while ( true )
fsFileToBeZipped.Close();
//prepare and delete file
fi.Attributes = FileAttributes.Normal;
//fi.Delete();
} //end try
catch (Exception e)
{
Console.WriteLine("Error zipping File. Error - " + e.Message);
} //end catch
}
答案 0 :(得分:1)
你的问题就在这里
new ZipEntry(fi.FullName);
zipEntry的参数是zip文件中的路径,而不是压缩数据的完整路径。通常,zip库(如7zip和SharpZip)提供了一种创建“入口路径”的方法,但写入zip的实际数据来自完整路径。
可能你想要的是
new ZipEntry(Path.GetFileName(fi.fullName))