Have some troubles with file package manipulation.
Here is short description of issue:
If a package contains a file with Unicode characters in it, compressing of it works well(can open package and be uncompressed it in windows) but if try to use this Package.Open(.., FileMode.Open), it results in error "File contains corrupted data".
Question: What am i doing wrong? Can't figure this out. Please advise.
Here is code for compressing and uncompromising binary data in-memory that i use:
public virtual byte[] UnZipFile(byte[] data, string packageFileName)
{
using (var inputStream = new MemoryStream(data))
{
using (var package = Package.Open(inputStream, FileMode.Open))
{
var fileUri = PackUriHelper.CreatePartUri(new Uri(@"/" + packageFileName, UriKind.Relative));
using (var outputStream = new MemoryStream())
{
using (var partStream = package.GetPart(fileUri).GetStream())
{
partStream.CopyTo(outputStream);
}
return outputStream.ToArray();
}
}
}
}
public virtual byte[] ZipFile(byte[] data, string packageFileName)
{
using (var outMs = new MemoryStream())
{
using (var package = Package.Open(outMs, FileMode.Create))
{
var fileUri = PackUriHelper.CreatePartUri(new Uri(@"/" + packageFileName, UriKind.Relative));
var contentType = @"data/" + packageFileName.Substring(packageFileName.LastIndexOf(".", StringComparison.Ordinal) + 1);
using (var zipStream = package.CreatePart(fileUri, contentType, CompressionOption.Normal).GetStream())
{
using (var ms = new MemoryStream(data))
{
ms.CopyTo(zipStream);
}
}
}
return outMs.ToArray();
}
}
solved by using newer lib, System.IO.Compression, thanks for pointing out this @Hans Passant