简体   繁体   中英

Append to a file in ZipPackage - .NET

I have a zip file that some files are stored in it. I need to append data to one of the files inside it some time after creating and writing into it.

    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);

    }

And this function creates the zip file for the first time:

    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);
    }

The ZipPackage.Open... in initFileForAppend throws this exception:

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: 

I tried using FileAccess.ReadWrite as third argument in ZipPackage.Open... in initFileForAppend but it didn't help.

PS: I don't want to rewrite the whole code just to use a 3rd party library. I prefer to use ZipPackage in .NET library.

I think you'll have to unzip the file, add to it, then re-zip, because of the way the zip algorithm works. Correct me if I'm wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM