copy file inside a zip into a new zip #835
Unanswered
MichaelJohnBradley
asked this question in
Q&A
Replies: 2 comments
-
I think it should be possible like this: using System.IO;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;
var tempFiles = new List<string>();
using var zf = new ZipFile("outputfile.zip");
using (var zis = new ZipInputStream(File.OpenRead("inputfile.zip"))) {
while (zis.GetNextEntry() is ZipEntry entry) {
// Let's say we only want to copy PDF files
if (entry.Name.EndsWith(".pdf")) {
// Extract the file we want to a temporary file
var tempFile = Path.GetTempFileName();
using (var fs = File.Create(tempFile)) {
zis.CopyTo(fs);
}
tempFiles.Add(tempFile);
// Add the new entry to the pending updates (does not perform the changes yet)
zf.Add(tempFile, entry.Name);
}
}
}
if (tempFiles.Count > 0) {
// Write the new entries to the output zip file
zf.CommitUpdate();
// Cleanup temp files
tempFiles.ForEach(f => File.Delete(f));
} You could avoid extracting the files you want to transfer as well, but it's a bit more complicated. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for that.
Would you know how to do the more complicated way to avoid extracting the file each time?
…________________________________
From: nils måsén ***@***.***>
Sent: 25 May 2023 16:46
To: icsharpcode/SharpZipLib ***@***.***>
Cc: Michael Bradley ***@***.***>; Author ***@***.***>
Subject: Re: [icsharpcode/SharpZipLib] copy file inside a zip into a new zip (Discussion #835)
I think it should be possible like this:
using System.IO;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;
var tempFiles = new List<string>();
using var zf = new ZipFile("outputfile.zip");
using (var zis = new ZipInputStream(File.OpenRead("inputfile.zip"))) {
while (zis.GetNextEntry() is ZipEntry entry) {
// Let's say we only want to copy PDF files
if (entry.Name.EndsWith(".pdf")) {
// Extract the file we want to a temporary file
var tempFile = Path.GetTempFileName();
using (var fs = File.Create(tempFile)) {
zis.CopyTo(fs);
}
tempFiles.Add(tempFile);
// Add the new entry to the pending updates (does not perform the changes yet)
zf.Add(tempFile, entry.Name);
}
}
}
if (tempFiles.Count > 0) {
// Write the new entries to the output zip file
zf.CommitUpdate();
// Cleanup temp files
tempFiles.ForEach(f => File.Delete(f));
}
You could avoid extracting the files you want to transfer as well, but it's a bit more complicated.
—
Reply to this email directly, view it on GitHub<#835 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AGZXAY33QDS7XQTZOYNIGMLXH55FTANCNFSM6AAAAAAYOU65MI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi
Could someone help explain how i can do the following.
In an existing zip file (that i have as a file stream) i need to loop through each file inside the zip/zip entry and maybe copy the file into a new zip file.
Is is possible to do this without decompressing each file?
Beta Was this translation helpful? Give feedback.
All reactions