我有一个仅上传zip文件的RadUpload, 所以我想在提取过程中重命名每个文件, 我尝试了一种方式:
Protected Sub Upload(sender As Object, e As EventArgs)
Dim extractPath As String = Server.MapPath("~/temp/")
Dim file1 As String = RadUpload1.UploadedFiles(0).FileName
ExtractFileToDirectory(file1, extractPath)
End Sub
Public Sub ExtractFileToDirectory(zipFileName As String, outputDirectory As String)
Dim zip As ZipFile = ZipFile.Read(outputDirectory & zipFileName)
Directory.CreateDirectory(outputDirectory)
For Each e As ZipEntry In zip
Dim NewName As String = Now().ToString("ddMMyyhhmmss")
Dim newext As String = ".jpg"
e.FileName = NewName + newext
e.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently)
Next
End Sub
首先它将重命名并解压缩第一个文件,但随后会出现此错误:
[已经调整组:无法执行人口普查程序。 ]
任何想法?
答案 0 :(得分:1)
outputDirectory
Dim zip As ZipFile = ZipFile.Read(outputDirectory & zipFileName)
Directory.CreateDirectory(outputDirectory)
在第一行中,您尝试阅读outputDirectory & zipFileName
,在第二行中您尝试创建该路径。
请参阅MSDN,您的代码应与
类似Using zip As ZipArchive = ZipFile.OpenRead(zipFileName)
For Each e As ZipArchiveEntry In zip.Entries
Dim NewName As String = Now().ToString("ddMMyyhhmmss")
Dim newext As String = ".jpg"
NewName += newext
e.ExtractToFile(Path.Combine(outputDirectory, NewName ))
Next
End Using
注意:使用"ddMMyyhhmmss"
作为文件名,如果解压缩时间少于1秒,则很可能会出错 - 要么添加ms,即"ddMMyyhhmmssfff"
,要么在提取之前检查文件名是否不存在
答案 1 :(得分:0)
因为你期望提取到一个" temp"文件夹,你真的应该使用System临时文件夹。在Web应用程序中编写数据是一种不好的做法。
使用以下方法检索系统定义的临时文件夹
Private Sub Upload(sender As Object, e As EventArgs)
Dim extractPath As String = System.IO.Path.GetTempPath() ' Better way to store temporary files
Dim file1 As String = RadUpload1.UploadedFiles(0).FileName
ExtractFileToDirectory(file1, extractPath)
End Sub
然后在ExtractFileToDirectory函数内部创建一个全新的临时文件夹(使用Guid.NewGuid()。ToString()生成一个唯一的文件夹名称)并使用ExtractToFile方法解压缩文件,如@ 2316116所述。
遵循此方法可以避免在同时解压缩多个zip文件时发生的令人讨厌的错误。