トップ回答者
System.IO.PackagingでZIP書庫を作成したいのですが...

質問
-
こんにちは
Win7+VS2008でZIP書庫を作成しようとしています。
System.IO.Packaging名前空間を使用して下記のような関数を作成しました。
この関数を実行すると、ZIP書庫自体は作成されるのですが、書庫に格納されるはずのファイルが書庫内に存在せず、
書庫内には[Content_Types].xmlというファイルしか存在せず、ファイル内容は以下の通りです。
<?xml version="1.0" encoding="utf-8" ?><Default Extension="xls" ContentType="" /></Types>CreatePart関数の第2引数に何を渡していいのかわからないので空文字を引数としているのが悪いのでしょうか?
壁にぶち当たっています。
どなたか、アドバイスをお願いいたします。
private bool test_CreateZIP()
{
try
{
// ZIP書庫を作成する
string packagePath = "C:\\Temp\\testCreateZip.zip";
Package package = Package.Open(@packagePath, FileMode.Create, FileAccess.ReadWrite);// 書庫に入れるファイル
byte[] buffer = new byte[1024];
int readSize = 0;string documentPath = "C:\\Temp\\testCreateZip.xls";
Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(@documentPath, UriKind.Relative));// CreatePart関数の第2引数は何を渡せばいいのだろう?
PackagePart packagePartDocument = package.CreatePart(partUriDocument, "", CompressionOption.Normal);
FileStream fs = new FileStream(documentPath, FileMode.Open, FileAccess.Read);while((readSize = fs.Read(buffer, 0, buffer.Count())) > 0)
packagePartDocument.GetStream().Write(buffer, 0, readSize);fs.Close();
package.Flush();
package.Close();
MessageBox.Show("created!!", "success");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "エラー");
return false;
}
}