F
Frank Prevatt
I have a C# class that has a method which takes a list of files and creates a
zip file using the system.io.packaging.package class. It is called from
another class in another project which creates the files to be packaged. This
in turn is called by a WCF service.
What I am finding is that when a single call is made to the zipping
component, it works fine. However, if it is called by different instances of
the component, it locks up on copying the stream to the package.
Here is the code for the ZipComponents method...with it's CopyStream
subroutine:
public void ZipComponents()
{
try
{
string zipFile = Path.ChangeExtension(fileName, "zip");
int fileNumber = 0;
using (Package exportPackage = Package.Open(zipFile,
FileMode.Create))
{
foreach (string file in componentFiles)
{
string tempFilePath =
file.Split("|".ToCharArray())[0];
string originalFileName =
file.Split("|".ToCharArray())[1];
fileNumber++;
Uri partUriDocument =
PackUriHelper.CreatePartUri(new Uri(fileNumber.ToString() + "-" +
originalFileName, UriKind.Relative));
PackagePart packagePart =
exportPackage.CreatePart(partUriDocument, "video/asf");
using (FileStream fileStream =
File.OpenRead(tempFilePath))
{
using (Stream packageStream =
packagePart.GetStream())
{
CopyStream(fileStream, packageStream);
}
}
string hashFile = Path.ChangeExtension(tempFilePath,
"hsh");
string originalHashName =
Path.ChangeExtension(originalFileName, "hsh");
partUriDocument = PackUriHelper.CreatePartUri(new
Uri(fileNumber.ToString() + "-" + originalHashName, UriKind.Relative));
packagePart =
exportPackage.CreatePart(partUriDocument, "text/hash");
using (FileStream fileStream =
File.OpenRead(hashFile))
{
using (Stream packageStream =
packagePart.GetStream())
{
CopyStream(fileStream, packageStream);
}
}
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry("Archive Service", "Error zipping " +
fileName + "\n" + ex.ToString(), EventLogEntryType.Error);
}
}
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
source.Flush();
target.Flush();
}
Any insight would be apprecitated.
zip file using the system.io.packaging.package class. It is called from
another class in another project which creates the files to be packaged. This
in turn is called by a WCF service.
What I am finding is that when a single call is made to the zipping
component, it works fine. However, if it is called by different instances of
the component, it locks up on copying the stream to the package.
Here is the code for the ZipComponents method...with it's CopyStream
subroutine:
public void ZipComponents()
{
try
{
string zipFile = Path.ChangeExtension(fileName, "zip");
int fileNumber = 0;
using (Package exportPackage = Package.Open(zipFile,
FileMode.Create))
{
foreach (string file in componentFiles)
{
string tempFilePath =
file.Split("|".ToCharArray())[0];
string originalFileName =
file.Split("|".ToCharArray())[1];
fileNumber++;
Uri partUriDocument =
PackUriHelper.CreatePartUri(new Uri(fileNumber.ToString() + "-" +
originalFileName, UriKind.Relative));
PackagePart packagePart =
exportPackage.CreatePart(partUriDocument, "video/asf");
using (FileStream fileStream =
File.OpenRead(tempFilePath))
{
using (Stream packageStream =
packagePart.GetStream())
{
CopyStream(fileStream, packageStream);
}
}
string hashFile = Path.ChangeExtension(tempFilePath,
"hsh");
string originalHashName =
Path.ChangeExtension(originalFileName, "hsh");
partUriDocument = PackUriHelper.CreatePartUri(new
Uri(fileNumber.ToString() + "-" + originalHashName, UriKind.Relative));
packagePart =
exportPackage.CreatePart(partUriDocument, "text/hash");
using (FileStream fileStream =
File.OpenRead(hashFile))
{
using (Stream packageStream =
packagePart.GetStream())
{
CopyStream(fileStream, packageStream);
}
}
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry("Archive Service", "Error zipping " +
fileName + "\n" + ex.ToString(), EventLogEntryType.Error);
}
}
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
source.Flush();
target.Flush();
}
Any insight would be apprecitated.