我有一个zip文件,其中存储了一些文件。我需要在创建和写入数据后的一段时间内将数据附加到其中的一个文件中。
public void initFileForAppend(string address)
{
if (outFile != null)
{
flush();
closeFile();
}
workingFile = address;
outFile = (ZipPackage)ZipPackage.Open(workingFile, FileMode.Append);
getStreamForAppend(OUTPUT_DATA_FILE_NAME);
getStreamForAppend(OFFSETS_FILE_NAME);
}
这个函数第一次创建了zip文件:
public override void initFile(string address)
{
if (outFile != null)
{
flush();
closeFile();
}
workingFile = address;
outFile = (ZipPackage)ZipPackage.Open(workingFile, FileMode.Create, FileAccess.ReadWrite);
getNewStream(OUTPUT_DATA_FILE_NAME);
getNewStream(OFFSETS_FILE_NAME, MediaTypeNames.Text.Plain);
getNewStream(SCENARIO_FILE_NAME, MediaTypeNames.Text.Xml);
}
ZipPackage.Open...
中的initFileForAppend
会引发此异常:
System.ArgumentException was unhandled by user code
HResult=-2147024809
Message=Append access can be requested only in write-only mode.
Source=mscorlib
StackTrace:
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync)
at MS.Internal.IO.Zip.ZipArchive.OpenOnFile(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode)
at SEWS.History.HistoryWriterEngine.initFileForAppend(String address) in e:\Radari\Phase 2\SEWS 2012\trunk\SEWS\History\HistoryWriterEngine.cs:line 46
at SEWS.MainForm.simulationStartPause() in e:\Radari\Phase 2\SEWS 2012\trunk\SEWS\MainForm.cs:line 539
at SEWS.MainForm.bgwSimulation_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) in e:\Radari\Phase 2\SEWS 2012\trunk\SEWS\MainForm.cs:line 1336
at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
InnerException:
我尝试在FileAccess.ReadWrite
的{{1}}中使用ZipPackage.Open...
作为第三个参数,但它没有帮助。
P.S:我不想仅仅为了使用第三方库而重写整个代码。我更喜欢在.NET库中使用initFileForAppend
。
答案 0 :(得分:1)
我认为你必须解压缩文件,添加它,然后重新压缩,因为zip算法的工作方式。如果我错了,请纠正我。