我想提取档案。 但问题是,当代码运行时,它会抛出以下异常:
System.IO.IOException:'文件'文件名'已经存在。'
以下是代码
File.WriteAllBytes(String_TempDir & "\rzip.zip", My.Resources.Resszip) 'I wrote the file from my application resources
Compression.ZipFile.ExtractToDirectory(String_TempDir & "\rzip.zip", String_TempDir) 'This line throws the exception
File.Delete(String_TempDir & "\rzip.zip")
在执行代码之前我没有看到任何内容(没有文件)......
执行代码后,它会抛出异常,但是,我的归档文件已被解压缩。
我使用Try语句来区分异常,但它仍然抛出异常...
Try
Compression.ZipFile.ExtractToDirectory(String_TempDir & "\rzip.zip", String_TempDir)
Catch ex As IOException
'That's it.
End Try
String_TempDir是一个字符串,我将其赋值为:
'global declaration:
Dim folder As String = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
'End of global declaration
Public Function GetTempDir() As String
Do While Directory.Exists(folder) Or File.Exists(folder)
folder = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
Loop
Return folder
End Function
'Form loads
Directory.CreateDirectory(folder)
String_TempDir = folder
答案 0 :(得分:0)
猜测,但可能是您将Zip文件放入要解压缩的同一目录中。尝试解压缩到临时目录的子目录。 e.g。
Compression.ZipFile.ExtractToDirectory(String_TempDir & "\rzip.zip", String_TempDir & "\extracted")
MSDN article on ExtractToDirectory表示以下内容(强调我的):
此方法创建指定的目录和所有子目录。 目标目录不能存在。与...相关的例外情况 验证destinationDirectoryName中的路径或 sourceArchiveFileName参数在提取之前被抛出。 否则,如果在提取期间发生错误,则归档仍然存在 部分提取。每个提取的文件具有相同的相对路径 destinationDirectoryName指定的目录作为其源 条目必须是存档的根目录。
答案 1 :(得分:0)
Have you also checked that the Zip file doesn't contain any duplicate names?
If it was compressed on Linux, it might have both Filename
and filename
inside it, and that might cause this error. Especially since you say that it doesn't contain any files at first and that it seems that it succeeds in decompressing it.
Somewhat similar question here, but with 7-Zip