vb.net 4.5 中的 Zipfile “目录名无效。”错误 - 无法确定原因?

Posted

技术标签:

【中文标题】vb.net 4.5 中的 Zipfile “目录名无效。”错误 - 无法确定原因?【英文标题】:Zipfile in vb.net 4.5 "The directory name is invalid." error - can't identify the cause? 【发布时间】:2013-06-16 16:41:02 【问题描述】:

我有一个小实用程序,可以使用 .net 4.5 TripleDES 例程进行加密/解密。我添加了压缩,发现更简单的压缩对我不起作用。

这些字段中引用的文件肯定存在,根本不是很大,甚至在相同的情况下命名。

但我不断收到“目录名称无效。”。如果我对存档的存在进行测试,我会得到一个不同的错误 - 因为它是 zip 并且它是 0 字节。与昨天弄清楚如何使用我的实用程序的加密部分相比,我已经搜索了更长的时间 - 那就是学习流的工作原理!

请问有人能解释一下这个问题吗?

Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"

Private Sub Main()
  Compress(Encoded, CompressEncoded)
End sub

Sub Compress(filename As String, zippedFile As String)
'Added to handle file already exists error
  If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
  If IO.File.Exists(filename) Then IO.Compression.ZipFile.CreateFromDirectory(filename, zippedFile, CompressionLevel.Fastest, True)
End Sub

除了这段代码之外,我还尝试添加一个快速流读取器测试来向自己证明文件名确实存在。 ' Dim sr As New StreamReader(filename) MessageBox.Show(sr.ReadToEnd)

它执行并显示其中的文本行。

如果能提供任何帮助,我将不胜感激 - 这个相当愚蠢的错误今天下午已经占用了我很多时间,我希望将其用于更有用的东西:)。

谢谢各位!

感谢您的回答!

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

Sub Decompress(ZippedFile As String, UnzippedFile As String)
    If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)

    If IO.File.Exists(ZippedFile) Then
        Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
            archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
        End Using
    End If
End Sub

【问题讨论】:

【参考方案1】:

这可能是一个简单的错误。 传递给CreateFromDirectory 的第一个参数应该是目录名,但您传递的是文件名。

试试

 If IO.File.Exists(filename) Then 
      IO.Compression.ZipFile.CreateFromDirectory(Path.GetDirectoryName(filename), _
                        zippedFile, CompressionLevel.Fastest, True)
 End If

【讨论】:

谢谢。不幸的是,它占用了整个文件夹的内容,然后立即用“该进程无法访问文件'C:\TestFile\encoded.zip',因为它正在被另一个进程使用。”大概是因为它实际上是在遍历文件并且需要更长的时间来压缩。正准备尝试其他解决方案。我认为我从根本上误解了 ZipFile 的工作原理——我认为它是输入文件、输出文件和一些重载文件。但是,它似乎并不那么简单 - 该链接确实暗示它是针对一个目录而不是单个文件。猜猜它的潜艇名称。 标记不是作为我查询的答案,而是因为它澄清了我对相关例程的轻微误解——这意味着我可以使用正确的工具来完成这项工作。谢谢!【参考方案2】:

如果要压缩单个文件,请使用ZipArchive 类,如下所示:

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = ZipFile.Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

附加信息

需要为ZipArchive 添加对System.IO.Compression.dll 的引用,为ZipFile 类添加System.IO.Compression.FileSystem.dll .在 VS 中创建项目时,默认不引用这些 DLL。

【讨论】:

@ajackblackgoat 谢谢。这解决了它,尽管根据我的重构专家,它需要对 system.io 的 .net 4 引用——直到我看到 ZipArchive 成员。我已经用我的最终实现更新了我的问题,所以谢谢。 是的。忘了告诉您需要为ZipArchive 添加对System.IO.Compression 的引用,为ZipFile 类添加对System.IO.Compression.FileSystem 的引用。跨度> @ajakblackgoat,我们正在尝试创建一个小型服务,以根据最新时间戳压缩和移动 oracle .dmp 文件,您的回答确实救了我们,否则我们正在为“CreateFromDirectory”而苦苦挣扎跨度>

以上是关于vb.net 4.5 中的 Zipfile “目录名无效。”错误 - 无法确定原因?的主要内容,如果未能解决你的问题,请参考以下文章

使用 vb.net 和 System.IO.Compression (.Net 4.5) 压缩文件

ZipFile未声明

python中的zipfile?

VB.Net 中 File.Copy 方法中的 IBM AppScan Security PathTraversal 问题

Python 文件权限中的 Zipfile

vb.net引用Dll的问题