1
\$\begingroup\$
public byte[] PackToZip(IEnumerable<byte[]> files)
 {
 using (var archiveStream = new MemoryStream())
 {
 using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
 {
 foreach (var file in files)
 {
 var entryName = string.Format("entry name");
 using (var entryStream = archive.CreateEntry(entryName).Open())
 {
 entryStream.Write(file, 0, file.Length);
 }
 }
 }
 return archiveStream.ToArray();
 }
 }

Is it safe, robust and efficient?

asked Nov 13, 2015 at 16:18
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Every file within the ZIP archive has the same path, so it would be awkward when extracting them. You should either take another parameter enumerating the entry names, or generate them automatically using a counter.

There's a limit to the scalability of this code, since the input and output all have to fit within memory. That problem can be avoided by accepting Streams instead of byte[]s for the input. You should also accept a Stream to which the output should be written. You could still do everything in memory by using MemoryStreams everywhere, but you would also have the flexibility to read from and write to files or the network.

answered Nov 13, 2015 at 16:42
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.