文件路径未获取 Zip
FilePath Not Getting Zip
我想压缩文件夹中的多个文件,但是我下面的代码可以正常工作,但它没有压缩文件,我不确定它得到空值时发生了什么。请指教
private static string filepath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["AConvert"])
? "" : ConfigurationManager.AppSettings["AConvert"];
static void Main(string[] args)
{
string zipFileName;
string fileExt;
try
{
fileExt = Path.GetExtension(filepath);
zipFileName = filepath.Replace(fileExt + DateTime.Now.ToString("MMyy"), ".zip");
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
{
s.Password = "ABC123";
s.SetLevel(4); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
ZipEntry entry = new ZipEntry(Path.GetFileName(filepath));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filepath))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
}
不确定您在此处输入的是什么,但我怀疑您可能想使用 Path.GetFileName() instead of Path.GetExtension。
您目前得到的只是一个空字符串,因为 "D:\Report" 没有扩展名。
此外,如果您想以这种方式压缩文件,我相信您必须对目录中的每个文件执行此操作,而不仅仅是对整个目录执行此操作。
我个人建议您改为查看 dotnetzip library。它有一些非常简单的方法来创建 zip 存档并向其中添加文件。在你的情况下,基本上是这样的:
var yourListOfFilePaths = Directory.GetFiles(pathToYourDir);
using (ZipFile zip = new ZipFile())
{
foreach(string filePath in yourListOfFilePaths)
{
zip.AddFile(filePath);
}
zip.Save(pathToTargetDir + "\MyZipFile.zip");
}
PS: 你可以找到更多 C# examples for DotNetZip here.
好的,伙计们,它现在可以工作了。这是我从 Kjartan 修改的代码。谢谢
private static string filepath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["AConvert"]) ? "" : ConfigurationManager.AppSettings["AConvert"];
private static string ZipPath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["PathZip"]) ? "" : ConfigurationManager.AppSettings["PathZip"];
static void Main(string[] args)
{
var yourListOfFilePaths = Directory.GetFiles(filepath);
using (ZipFile zip = new ZipFile())
{
foreach (string filePath in yourListOfFilePaths)
{
zip.AddFile(filePath);
}
zip.Save(ZipPath + "\Batch" + DateTime.Now.ToString("ddmmyy") + ".zip");
}
我想压缩文件夹中的多个文件,但是我下面的代码可以正常工作,但它没有压缩文件,我不确定它得到空值时发生了什么。请指教
private static string filepath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["AConvert"])
? "" : ConfigurationManager.AppSettings["AConvert"];
static void Main(string[] args)
{
string zipFileName;
string fileExt;
try
{
fileExt = Path.GetExtension(filepath);
zipFileName = filepath.Replace(fileExt + DateTime.Now.ToString("MMyy"), ".zip");
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
{
s.Password = "ABC123";
s.SetLevel(4); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
ZipEntry entry = new ZipEntry(Path.GetFileName(filepath));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filepath))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
}
不确定您在此处输入的是什么,但我怀疑您可能想使用 Path.GetFileName() instead of Path.GetExtension。
您目前得到的只是一个空字符串,因为 "D:\Report" 没有扩展名。
此外,如果您想以这种方式压缩文件,我相信您必须对目录中的每个文件执行此操作,而不仅仅是对整个目录执行此操作。
我个人建议您改为查看 dotnetzip library。它有一些非常简单的方法来创建 zip 存档并向其中添加文件。在你的情况下,基本上是这样的:
var yourListOfFilePaths = Directory.GetFiles(pathToYourDir);
using (ZipFile zip = new ZipFile())
{
foreach(string filePath in yourListOfFilePaths)
{
zip.AddFile(filePath);
}
zip.Save(pathToTargetDir + "\MyZipFile.zip");
}
PS: 你可以找到更多 C# examples for DotNetZip here.
好的,伙计们,它现在可以工作了。这是我从 Kjartan 修改的代码。谢谢
private static string filepath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["AConvert"]) ? "" : ConfigurationManager.AppSettings["AConvert"];
private static string ZipPath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["PathZip"]) ? "" : ConfigurationManager.AppSettings["PathZip"];
static void Main(string[] args)
{
var yourListOfFilePaths = Directory.GetFiles(filepath);
using (ZipFile zip = new ZipFile())
{
foreach (string filePath in yourListOfFilePaths)
{
zip.AddFile(filePath);
}
zip.Save(ZipPath + "\Batch" + DateTime.Now.ToString("ddmmyy") + ".zip");
}