SharpZipLib icon indicating copy to clipboard operation
SharpZipLib copied to clipboard

Exception thrown when attempting to create tar archive from files

Open czielin opened this issue 8 years ago • 9 comments

The following exception is thrown when attempting to create a tar archive:

ICSharpCode.SharpZipLib.Tar.TarException: 
Entry closed at '0' before the '986120' bytes specified in the header were written
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.CloseEntry()
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.Finish()
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.Close()
   at ICSharpCode.SharpZipLib.Tar.TarArchive.Dispose(Boolean disposing)
   at ICSharpCode.SharpZipLib.Tar.TarArchive.Dispose()
   at CreateTarArchive(Stream tempFile, IEnumerable`1 files)

Using the following code:

private void CreateTarArchive(Stream tempFile, IEnumerable<string> files)
{
    using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(tempFile))
    {
        foreach (string file in files)
        {
            string filePath = Path.Combine(productFileLocation, file);
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(filePath);
            tarEntry.Name = file;
            tarArchive.WriteEntry(tarEntry, false);
        }
    }
}

The error seems to be sporadic. Sometimes it will succeed in creating the archive without error.

Thanks in advance for any help.

Steps to reproduce

  1. See above code sample.

Expected behavior

A tar archive should be created without error.

Actual behavior

Exception is thrown (see above).

Version of SharpZipLib

0.86.0 1.0.0

Obtained from (place an x between the brackets for all that apply)

  • Package installed using:
    • NuGet

czielin avatar May 07 '18 17:05 czielin

Have you tried using the newer alpha packages?

piksel avatar May 12 '18 10:05 piksel

Sorry. The issue mysteriously disappeared for quite a while. It has just as mysteriously cropped up again. I updated from 0.86.0 to 1.0.0, but it didn't make a difference.

czielin avatar Aug 25 '18 07:08 czielin

The error stems from the input source being first examined to be 986120 bytes in size here: https://github.com/icsharpcode/SharpZipLib/blob/8feb39a0c26e83b80e7fb0c481834e88d35297ee/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs#L388 But when being piped into the stream here: https://github.com/icsharpcode/SharpZipLib/blob/8feb39a0c26e83b80e7fb0c481834e88d35297ee/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs#L709 no bytes are being read.

What is the source of your input files? Could there be some kind of racing issue?

piksel avatar Aug 25 '18 07:08 piksel

Thanks for the quick reply.

I'm not quite sure what condition might cause FileInfo to return a non-zero value for file size, but then read zero bytes immediately after. There is nothing in our code that should be emptying the content of the file.

I tried to create a small project to reproduce, but was unable to. The offending code is running inside a Windows service. The sporadic nature of the error also makes me think there may be some race condition going on.

The input files are images in the ntif format.

czielin avatar Aug 25 '18 17:08 czielin

I'm seeing the same issue:

ICSharpCode.SharpZipLib.Tar.TarException: Entry closed at '0' before the '1377' bytes specified in the header were written
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.CloseEntry()
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.Finish()
   at ICSharpCode.SharpZipLib.Tar.TarOutputStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at ICSharpCode.SharpZipLib.Tar.TarArchive.Dispose(Boolean disposing)
   at ICSharpCode.SharpZipLib.Tar.TarArchive.Dispose()

I have a clue that might help. I saw this while trying to include an open file into a .tar.gz. The file that it died on is a debug log that is open and being written to by log4net at the same time SharpZipLib is trying to archive it. When I change my code to skip over the file I know is open, everything works fine.

My theory: SharpZipLib is confused by the file being open. Maybe it uses the directory entry to figure out the file's size, but is unable to read the same number of bytes (not yet flushed to disk)? Or maybe SharpZipLib is trying to open exclusive but failing, so it can't read any bytes.

cosinekitty avatar Sep 21 '18 21:09 cosinekitty

@cosinekitty thanks for the suggestion.

I believe my issue may be similar. The files being archived are being created by a different component of the application. Sometimes immediately before the archive is attempting to be built.

I refactored the component creating the files in attempt to make sure it is releasing the file lock earlier. This seems to have solved the problem for now. I haven't seen it reoccur in about a month now.

czielin avatar Oct 06 '18 17:10 czielin

@piksel thanks as well for the help. This is probably safe to close once again. I will chime back in if it again becomes an issue.

czielin avatar Oct 06 '18 17:10 czielin

I get the same errors as "Entry closed at 32768 before the 35000 bytes specified in the header were written" or "...0 before the 15000..." always on a small % of organization PC. And never on the other part of PC.

The code based on the official samples and not parallel at all. It uses MemoryStream to protect against external file using

var myMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes("Up to 1,000,000 text or binary data"));

MemoryStream tarMemStream = new MemoryStream();
using (var tar = new TarOutputStream(tarMemStream, Encoding.UTF8))
{
      var tarEntry = TarEntry.CreateTarEntry("anyPath");
      tarEntry.Size = myMemoryStream.Length - myMemoryStream.Position;
      tarStream.PutNextEntry(tarEntry);
      byte[] localBuffer = new byte[32768];
      while (true)
      {
            int numRead = myMemoryStream.Read(localBuffer, 0, localBuffer.Length);
            if (numRead <= 0)
                  break;

            tarStream.Write(localBuffer, 0, numRead);
      }
      tarStream.CloseEntry();
}

I have no idea why it doesn't even work with MemoryStream.

It may be helpful that the application on this PC may also throw the following error when working with files: The handle with which this oplock was associated has been closed. The oplock is now broken. (Exception from HRESULT: 0x80070323)

Wufus avatar Jul 12 '24 06:07 Wufus

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

SourceproStudio avatar Jul 12 '24 06:07 SourceproStudio