1

I have a method that extracts multiple zip files but can't delete them until application close. I think it's a bug in DotNetZip version 1.9.1.5 library.

my zip files:

Hezareh.z01
Hezareh.z02
Hezareh.z03
Hezareh.zip

code to extract files:

        private void InstallUpdates()
        {
            string tempDir = Path.Combine(Environment.CurrentDirectory, MainViewModel.TemporaryDirectoryName);
            string mainZipFileName = Path.Combine(tempDir, 
                _updateConfig.ZipFiles.Single(f => f.IsFirstPart).PathOnClient); // i'm sure this is Hezareh.zip file
            string installPath = Path.Combine(Environment.CurrentDirectory, Properties.Settings.Default.InstallationDirectory);
            // i also tried: using (var zipFile = new ZipFile(mainZipFileName)) 
            using (var zipFile = ZipFile.Read(mainZipFileName))
            {
                // same problem for ExtractAll method.
                // zipFile.ExtractAll(installPath, ExtractExistingFileAction.OverwriteSilently);
                foreach (ZipEntry entiry in zipFile)
                {
                    entiry.Extract(installPath, ExtractExistingFileAction.OverwriteSilently);
                }
                // zipFile.Close() or any same method does not exists and zipFile.Dispose() is called
                // automatically but even when i call it manually still have same problem.
            }

            DeleteFiles();
        }
        private void DeleteFiles()
        {
            var files = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, MainViewModel.TemporaryDirectoryName));
            foreach (string file in files)
            {
                File.Delete(file); // exception occure here 
            }
        }

This question did not work for me and I'm going to burn my head.

Exception:

 The process cannot access the file 'D:\Projects\...\App\bin\Debug\UpdaterTemp\Hezareh.z01' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)
   at Meshkat.Updater.App.ViewModels.InstallUpdatesViewModel.DeleteDownloadedUpdates() in D:\Projects\Meshkat.Updater\Meshkat.Updater.App\ViewModels\InstallUpdatesViewModel.cs:line 60
   at Meshkat.Updater.App.ViewModels.InstallUpdatesViewModel.Hide() in D:\Projects\Meshkat.Updater\Meshkat.Updater.App\ViewModels\InstallUpdatesViewModel.cs:line 129
   at Meshkat.Updater.App.RelayCommand.Execute(Object parameter) in D:\Projects\Meshkat.Updater\Meshkat.Updater.App\RelayCommand.cs:line 146
   at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at Meshkat.Updater.App.App.Main() in D:\Projects\Meshkat.Updater\Meshkat.Updater.App\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
Community
  • 1
  • 1
Jalal
  • 6,594
  • 9
  • 63
  • 100
  • 2
    What does `DeleteFiles()` do? Show some code. What is the error? Are there any? – leppie Jun 05 '12 at 12:05
  • 1
    Also, what version of DotNetZip are you using? Versions prior to v1.9.1.6 had a bug where you couldn't delete files after you extracted. Perhaps you need to upgrade? But tell us your version. – Faraday Jun 05 '12 at 12:08
  • 1
    Are you unzipping files only to delete them immediately? Are you perhaps doing something with them that you are not showing? Or does your code monitor the output path for changes? Instead of looking for a bug in the library you should ensure that your code isn't still working with the files when you try to delete them – Panagiotis Kanavos Jun 05 '12 at 12:09
  • @Vijay: Please post your comment to Mark as Answer :) Thank You man! – Jalal Jun 05 '12 at 12:13
  • An alternative would be to use the SharpZipLib library for extraction, it doesn't have any problems like this and the usage is pretty much the same. http://www.icsharpcode.net/opensource/sharpziplib/ it's opensource too so there's no licensing issues. – Jason Larke Jun 05 '12 at 12:14

1 Answers1

4

Versions prior to v1.9.1.6 had a bug where you couldn't delete files after you extracted. Perhaps you need to upgrade? But tell us your version.

For more information see this WorkItem on CodePlex: http://dotnetzip.codeplex.com/workitem/12521

Faraday
  • 2,904
  • 3
  • 23
  • 46
  • Still you need to use Dispose() after extract to close the stream. It was not obvious to me than this method was actually a "Close". – job3dot5 Mar 14 '19 at 14:47
  • This issue still exists, is there a workaround with dotnetzip? version 1.13.3 – AlexS May 07 '19 at 12:26