G
G Britton
Guest
Im using .net 3.5 (cant use 4.x in this case) and the System.IO.Compression library. Im using it to compress an Excel file into a ZIP file. Things have been going well until yesterday, when the Excel file crossed some sort of size boundary. Since then Im getting this exception:
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ObjectDisposedException: Can not access a closed Stream.
at System.IO.Compression.DeflateStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.Save(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipArchive.Dispose(Boolean disposing)
at System.IO.Packaging.ZipPackage.Dispose(Boolean disposing)
at System.IO.Packaging.Package.System.IDisposable.Dispose()
at ST_f6fc9387cbce4d7780ba4248547a4e6e.csproj.ScriptMain.AddFileToZip(String zipFilename, String fileToAdd)
at ST_f6fc9387cbce4d7780ba4248547a4e6e.csproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
The code I use is:
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipFile
{
class Program
{
static void Main(string[] args)
{
var fileToAdd = @"S:\SourceData\ExtendedSupport\Output\ExtendedSupport_2016-04-07.xls";
var zipFilename = Path.ChangeExtension(fileToAdd, ".zip");
AddFileToZip(zipFilename, fileToAdd);
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}
private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
{
long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesWritten = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
}
}
}
}
Has anyone seen this? Is this a bug in my code or something else?
Continue reading...
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ObjectDisposedException: Can not access a closed Stream.
at System.IO.Compression.DeflateStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.Save(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipArchive.Dispose(Boolean disposing)
at System.IO.Packaging.ZipPackage.Dispose(Boolean disposing)
at System.IO.Packaging.Package.System.IDisposable.Dispose()
at ST_f6fc9387cbce4d7780ba4248547a4e6e.csproj.ScriptMain.AddFileToZip(String zipFilename, String fileToAdd)
at ST_f6fc9387cbce4d7780ba4248547a4e6e.csproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
The code I use is:
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipFile
{
class Program
{
static void Main(string[] args)
{
var fileToAdd = @"S:\SourceData\ExtendedSupport\Output\ExtendedSupport_2016-04-07.xls";
var zipFilename = Path.ChangeExtension(fileToAdd, ".zip");
AddFileToZip(zipFilename, fileToAdd);
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}
private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
{
long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesWritten = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
}
}
}
}
Has anyone seen this? Is this a bug in my code or something else?
Continue reading...