I've created à Zip maker in VB.NET and I've this error :
You can't create entries as long as previously created entries are still open.
(Translated from French : Impossible de créer des entrées tant que les entrées créées précédemment sont toujours ouvertes.)
My code is :
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
I use .NET Framework 4.5 in VS 2010 and I've imported System.IO.Compression in my Class
Can someone help me please ?
The code to do what you want can be much simpler than that. Reference both System.IO.Compression.dll and System.IO.Compression.FileSystem.dll and then do this:
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
That will name each entry using the file name only. If you want the full file path, omit the Path.GetFileName
call.
You might also look at the ZipFile.CreateFromDirectory
method to see whether that will do what you want in a single call.