1
Vote

zero (0) byte files in 7z throw error when uncompressing

description

I am trying to upzip a 7z file that may or may not contain a 0 byte file which will later be qualified. When trying to write the 0 byte file, an error is thrown. How can I write this file short of just creating the file by name?

using System;
using System.Configuration;
using System.IO;
using SharpCompress.Archive;
using SharpCompress.Common;

public string UnzipFile(string filename)
{
 //create temp directory
 string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName().Replace(".", ""));

 try
 {
      Directory.CreateDirectory(tempDirectory);
 }
 catch (Exception ex)
 {
      throw;;
 }

 try
 {
      var archive = ArchiveFactory.Open(filename);
      foreach (var entry in archive.Entries)
      {
           if (!entry.IsDirectory)
           {
                Console.WriteLine(entry.FilePath);
                 entry.WriteToDirectory(tempDirectory, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
           }
      }
 }
 catch (Exception ex)
 {
      throw;
 }

 return tempDirectory;
}

file attachments

comments

adamhathcock wrote Mar 25, 2015 at 12:51 PM

Could you move this to github?

Also post a stack trace.

Thanks

rhino1130 wrote Mar 25, 2015 at 2:50 PM

no github.

Stack trace doesn't show anything. SharpCompress is available in nugget.

rhino1130 wrote Mar 25, 2015 at 3:08 PM

I would expect that the
 entry.WriteToDirectory(tempDirectory, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
above would write the filename and zero byte file. It doesn't; it throws an System.NullReferenceException exception, {"Object reference not set to an instance of an object."}.

And, sorry, I did get the stack trace:

at SharpCompress.IO.ListeningStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at SharpCompress.Utility.Extract[TEntry,TVolume](TEntry entry, AbstractArchive`2 archive, Stream streamToWriteTo)
at SharpCompress.Archive.SevenZip.SevenZipArchiveEntry.WriteTo(Stream stream)
at SharpCompress.Archive.IArchiveEntryExtensions.WriteToFile(IArchiveEntry entry, String destinationFileName, ExtractOptions options)
at SharpCompress.Archive.IArchiveEntryExtensions.WriteToDirectory(IArchiveEntry entry, String destinationDirectory, ExtractOptions options)
at mytestapp.ZipUtility.UnzipFile(String filename) in c:\Code\mytestapp\FileUtilities\ZipUtility.cs:line 55

rhino1130 wrote Mar 25, 2015 at 4:26 PM

pls ignore the comment @ 10:08. Didn't realize who you were. Any suggestions?