mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-23 07:25:11 +00:00
Merge pull request #138 from adamhathcock/leaveopen_writers
Leave open writers
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
using SharpCompress.Reader;
|
||||
using SharpCompress.Writer;
|
||||
|
||||
@@ -18,9 +20,15 @@ namespace SharpCompress.Test
|
||||
{
|
||||
ResetScratch();
|
||||
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
|
||||
using (var writer = WriterFactory.Open(stream, type, compressionType))
|
||||
{
|
||||
writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
|
||||
using (var writer = WriterFactory.Open(stream, type, compressionType, true))
|
||||
{
|
||||
writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
|
||||
}
|
||||
if (!stream.CanWrite)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));
|
||||
|
||||
@@ -27,6 +27,11 @@ namespace SharpCompress.Compressor.BZip2
|
||||
this.stream = new CBZip2InputStream(stream, decompressContacted, leaveOpen);
|
||||
}
|
||||
}
|
||||
|
||||
public void Finish()
|
||||
{
|
||||
(this.stream as CBZip2OutputStream)?.Finish();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace SharpCompress.Reader
|
||||
if (BZip2Stream.IsBZip2(rewindableStream))
|
||||
{
|
||||
rewindableStream.Rewind(false);
|
||||
BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
|
||||
BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Tar.Headers;
|
||||
using SharpCompress.Compressor;
|
||||
using SharpCompress.Compressor.Deflate;
|
||||
|
||||
@@ -11,10 +10,10 @@ namespace SharpCompress.Writer.GZip
|
||||
{
|
||||
private bool wroteToStream;
|
||||
|
||||
public GZipWriter(Stream destination)
|
||||
public GZipWriter(Stream destination, bool leaveOpen = false)
|
||||
: base(ArchiveType.GZip)
|
||||
{
|
||||
InitalizeStream(new GZipStream(destination, CompressionMode.Compress, true), true);
|
||||
InitalizeStream(new GZipStream(destination, CompressionMode.Compress, leaveOpen), !leaveOpen);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace SharpCompress.Writer.Tar
|
||||
{
|
||||
public class TarWriter : AbstractWriter
|
||||
{
|
||||
public TarWriter(Stream destination, CompressionInfo compressionInfo)
|
||||
public TarWriter(Stream destination, CompressionInfo compressionInfo, bool leaveOpen = false)
|
||||
: base(ArchiveType.Tar)
|
||||
{
|
||||
if (!destination.CanWrite)
|
||||
@@ -23,12 +23,12 @@ namespace SharpCompress.Writer.Tar
|
||||
break;
|
||||
case CompressionType.BZip2:
|
||||
{
|
||||
destination = new BZip2Stream(destination, CompressionMode.Compress, false);
|
||||
destination = new BZip2Stream(destination, CompressionMode.Compress, leaveOpen);
|
||||
}
|
||||
break;
|
||||
case CompressionType.GZip:
|
||||
{
|
||||
destination = new GZipStream(destination, CompressionMode.Compress, false);
|
||||
destination = new GZipStream(destination, CompressionMode.Compress, leaveOpen);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -36,7 +36,7 @@ namespace SharpCompress.Writer.Tar
|
||||
throw new InvalidFormatException("Tar does not support compression: " + compressionInfo.Type);
|
||||
}
|
||||
}
|
||||
InitalizeStream(destination, false);
|
||||
InitalizeStream(destination, !leaveOpen);
|
||||
}
|
||||
|
||||
public override void Write(string filename, Stream source, DateTime? modificationTime)
|
||||
@@ -91,7 +91,7 @@ namespace SharpCompress.Writer.Tar
|
||||
{
|
||||
PadTo512(0, true);
|
||||
PadTo512(0, true);
|
||||
OutputStream.Dispose(); // required when bzip2 compression is used
|
||||
(OutputStream as BZip2Stream)?.Finish(); // required when bzip2 compression is used
|
||||
}
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
||||
@@ -9,15 +9,15 @@ namespace SharpCompress.Writer
|
||||
{
|
||||
public static class WriterFactory
|
||||
{
|
||||
public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionType compressionType)
|
||||
public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionType compressionType, bool leaveOpen = false)
|
||||
{
|
||||
return Open(stream, archiveType, new CompressionInfo
|
||||
{
|
||||
Type = compressionType
|
||||
});
|
||||
}, leaveOpen);
|
||||
}
|
||||
|
||||
public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionInfo compressionInfo)
|
||||
public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionInfo compressionInfo, bool leaveOpen = false)
|
||||
{
|
||||
switch (archiveType)
|
||||
{
|
||||
@@ -27,15 +27,15 @@ namespace SharpCompress.Writer
|
||||
{
|
||||
throw new InvalidFormatException("GZip archives only support GZip compression type.");
|
||||
}
|
||||
return new GZipWriter(stream);
|
||||
return new GZipWriter(stream, leaveOpen);
|
||||
}
|
||||
case ArchiveType.Zip:
|
||||
{
|
||||
return new ZipWriter(stream, compressionInfo, null);
|
||||
return new ZipWriter(stream, compressionInfo, null, leaveOpen);
|
||||
}
|
||||
case ArchiveType.Tar:
|
||||
{
|
||||
return new TarWriter(stream, compressionInfo);
|
||||
return new TarWriter(stream, compressionInfo, leaveOpen);
|
||||
}
|
||||
default:
|
||||
{
|
||||
|
||||
@@ -24,13 +24,13 @@ namespace SharpCompress.Writer.Zip
|
||||
private readonly string zipComment;
|
||||
private long streamPosition;
|
||||
|
||||
public ZipWriter(Stream destination, CompressionInfo compressionInfo, string zipComment)
|
||||
public ZipWriter(Stream destination, CompressionInfo compressionInfo, string zipComment, bool leaveOpen = false)
|
||||
: base(ArchiveType.Zip)
|
||||
{
|
||||
this.zipComment = zipComment ?? string.Empty;
|
||||
|
||||
this.zipCompressionInfo = new ZipCompressionInfo(compressionInfo);
|
||||
InitalizeStream(destination, false);
|
||||
InitalizeStream(destination, !leaveOpen);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
|
||||
Reference in New Issue
Block a user