Remove an entry from a zip file #351

Open
opened 2026-01-29 22:10:33 +00:00 by claunia · 3 comments
Owner

Originally created by @fjjo23 on GitHub (Apr 26, 2019).

I need to remove an entry from a zip file and save it with a same name. I'm getting an error that the zip file is in use. As a workaround I'm saving the modified zip file with a different name, removing the original zip file and then renaming the modified zip file to the original name.
Is there any other way of doing this?
Thanks.

Originally created by @fjjo23 on GitHub (Apr 26, 2019). I need to remove an entry from a zip file and save it with a same name. I'm getting an error that the zip file is in use. As a workaround I'm saving the modified zip file with a different name, removing the original zip file and then renaming the modified zip file to the original name. Is there any other way of doing this? Thanks.
claunia added the question label 2026-01-29 22:10:33 +00:00
Author
Owner

@adamhathcock commented on GitHub (Apr 29, 2019):

You have to be more specific, probably with code. Yes, you can do this by loading everything into memory and disposing things properly.

@adamhathcock commented on GitHub (Apr 29, 2019): You have to be more specific, probably with code. Yes, you can do this by loading everything into memory and disposing things properly.
Author
Owner

@fjjo23 commented on GitHub (Apr 29, 2019):

Hi,
Thank you for the respond. This is how I’m doing it right now

private void RemovePolicyFileFromPolicyContainer(string p_policyContainer, string p_policyFile)
{
using (var archive = ZipArchive.Open(p_policyContainer))
{
var entry = archive.Entries.Single(p_o => p_o.Key == p_policyFile);

            archive.RemoveEntry(entry);

            if (archive.Entries.Count > 0)
            {
                archive.SaveTo($@"{p_policyContainer}_temp", new WriterOptions(CompressionType.Deflate));
            }
        }

        scFilesAndFolders.SafeDeleteFile(p_policyContainer);

        if (File.Exists($@"{p_policyContainer}_temp"))
        {
            File.Move($@"{p_policyContainer}_temp", p_policyContainer);
        }

}

I would like to avoid saving the modified zip file to a new file, deleting the modified file and renaming the new file to the original zip file

Thanks

From: Adam Hathcock notifications@github.com
Sent: Monday, April 29, 2019 3:06 AM
To: adamhathcock/sharpcompress sharpcompress@noreply.github.com
Cc: Fredi Jaramillo FJaramillo@Steelcloud.com; Author author@noreply.github.com
Subject: Re: [adamhathcock/sharpcompress] Remove an entry from a zip file (#449)

You have to be more specific, probably with code. Yes, you can do this by loading everything into memory and disposing things properly.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/adamhathcock/sharpcompress/issues/449#issuecomment-487471418, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AE4WVF2D5Y35PHTU2KUMGE3PS2M4XANCNFSM4HIXCNYQ.

@fjjo23 commented on GitHub (Apr 29, 2019): Hi, Thank you for the respond. This is how I’m doing it right now private void RemovePolicyFileFromPolicyContainer(string p_policyContainer, string p_policyFile) { using (var archive = ZipArchive.Open(p_policyContainer)) { var entry = archive.Entries.Single(p_o => p_o.Key == p_policyFile); archive.RemoveEntry(entry); if (archive.Entries.Count > 0) { archive.SaveTo($@"{p_policyContainer}_temp", new WriterOptions(CompressionType.Deflate)); } } scFilesAndFolders.SafeDeleteFile(p_policyContainer); if (File.Exists($@"{p_policyContainer}_temp")) { File.Move($@"{p_policyContainer}_temp", p_policyContainer); } } I would like to avoid saving the modified zip file to a new file, deleting the modified file and renaming the new file to the original zip file Thanks From: Adam Hathcock <notifications@github.com> Sent: Monday, April 29, 2019 3:06 AM To: adamhathcock/sharpcompress <sharpcompress@noreply.github.com> Cc: Fredi Jaramillo <FJaramillo@Steelcloud.com>; Author <author@noreply.github.com> Subject: Re: [adamhathcock/sharpcompress] Remove an entry from a zip file (#449) You have to be more specific, probably with code. Yes, you can do this by loading everything into memory and disposing things properly. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub<https://github.com/adamhathcock/sharpcompress/issues/449#issuecomment-487471418>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AE4WVF2D5Y35PHTU2KUMGE3PS2M4XANCNFSM4HIXCNYQ>.
Author
Owner

@adamhathcock commented on GitHub (Aug 20, 2019):

SaveTo a MemoryStream instead of a file

@adamhathcock commented on GitHub (Aug 20, 2019): SaveTo a MemoryStream instead of a file
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#351