gpt4 book ai didi

C# 创建包含许多文件的 ZipArchive

转载 作者:行者123 更新时间:2023-11-30 18:24:17 26 4
gpt4 key购买 nike

我的方法接受一个列表并创建一个 ZIP 文件和一个日志文件。它适用于较小数量的文件。但是当我获取大量文件(测试了 11,000 个)时,它会引发 SystemOutOfMemory 异常。

通过研究,我了解到我的方法可能会给内存带来很大的负担。所以我放入了部分是我刷新了 streamwriter 和 zip 存档。我可能必须对文件流做些什么。

解决这个问题的有效方法是什么?

代码如下:

        public static void BackupFilesToZip(string directory, string fileFilter, string zipFilePath, bool backupInSubDir, string logFilePath, List<FileInfo> filesToBackup)
{
FileInfo logFile = new FileInfo(logFilePath);
FileInfo zipFile = new FileInfo(zipFilePath);
int numberOfFiles = filesToBackup.Count;

if (!Directory.Exists(zipFile.DirectoryName)) Directory.CreateDirectory(zipFile.DirectoryName);

using (FileStream zipToOpen = new FileStream(zipFile.FullName, FileMode.OpenOrCreate))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(logFile.Name);
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("This ZIP archive was created: " + DateTime.Now.ToString("MM/dd/yy HH:mm:ss"));
writer.WriteLine("ZIP File: " + zipFilePath);
writer.WriteLine("Source Directory: " + directory);
string backupInSubText = "yes";
if (!backupInSubDir) backupInSubText = "no";
writer.WriteLine("Subdirectories included: " + backupInSubText);
writer.WriteLine("Filter Critera: " + fileFilter);
writer.WriteLine("Number of Files selected: " + numberOfFiles + " (for # of files archived/skipped scroll down)");
writer.WriteLine("");
writer.WriteLine("File Log:");

int filesArchivedCounter = 0;
int filesSkippedCounter = 0;
int filesSum = 0;

TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);

foreach (FileInfo file in filesToBackup)
{
//ZipArchiveEntry readmeEntry = archive.CreateEntry(logFile.Name);
string DateTimeStampBegin = DateTime.Now.ToString("MM/dd/yy HH:mm:ss");

try
{
string relativePath = MakeRelativePath(directory, file.FullName);
archive.CreateEntryFromFile(file.FullName, relativePath);
writer.WriteLine(DateTimeStampBegin + " - " + DateTime.Now.ToString("HH:mm:ss") + " archived: " + file.FullName);
filesArchivedCounter++;
}
catch
{
writer.WriteLine(DateTimeStampBegin + " - " + " SKIPPED: " + file.FullName);
filesSkippedCounter++;
}

filesSum = filesSkippedCounter + filesArchivedCounter;
TaskbarManager.Instance.SetProgressValue(filesSum, numberOfFiles);

//write from memory to files every 75 items (to avoid out of memory exception)
if (filesSum % 75 == 0)
{
writer.Flush();
zipToOpen.Flush();
}
}

writer.WriteLine("");
writer.WriteLine("# of Files archived: " + filesArchivedCounter);
writer.WriteLine("# of Files skipped: " + filesSkippedCounter);
}

if (!String.IsNullOrEmpty(logFile.Name))
{
if (!Directory.Exists(logFile.DirectoryName)) Directory.CreateDirectory(logFile.DirectoryName);
readmeEntry.ExtractToFile(logFile.FullName, true);
}

TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
}
}
}

该方法的所有字符串参数只针对日志文件。

最佳答案

您实现中的问题是您将日志条目放在存档的开头并在添加新文件后更新它,因此无法刷新 zip。您应该将日志写入文件,而不是存档,并在添加所有文件后将其添加到存档。

关于C# 创建包含许多文件的 ZipArchive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com