Error: You can't create entries as long as previously created entries are still open

Error: You can't create entries as long as previously created entries are still open

我在 VB.NET 中使用 Zip maker 创建了这个错误:

You can't create entries as long as previously created entries are still open.

(翻译自英文:无法创建条目,而以前创建的条目仍处于打开状态。)

我的代码是:

Dim filearchive As FileStream = New FileStream(My.Settings.archive_path, FileMode.CreateNew)
Dim archive As ZipArchive = New ZipArchive(filearchive, ZipArchiveMode.Create)
For Each File In FileIO.FileSystem.GetFiles(My.Settings.contacts_path)
    Dim crentry As ZipArchiveEntry = archive.CreateEntry(File)
    filearchive.CopyTo(crentry.Open())
    ProgressBar1.Increment(1)
    Label3.Text = ProgressBar1.Value.ToString + " %"
Next

我在 VS 2010 中使用 .NET Framework 4.5,并且在我的 Class

中导入了 System.IO.Compression

有人可以帮我吗?

执行您想要的操作的代码可以比这简单得多。参考 System.IO.Compression.dllSystem.IO.Compression.FileSystem.dll 然后这样做:

Using archive = ZipFile.Open(My.Settings.archive_path, ZipArchiveMode.Create)
    For Each filePath In Directory.EnumerateFiles(My.Settings.contacts_path)
        archive.CreateEntryFromFile(filePath, Path.GetFileName(filePath))
        '...
    Next
End Using

这将仅使用文件名命名每个条目。如果您想要完整的文件路径,请省略 Path.GetFileName 调用。

您还可以查看 ZipFile.CreateFromDirectory 方法,看看它是否会在一次调用中完成您想要的操作。