System.IO.Compression.ZipArchive.CreateEntry C# (CSharp) Method

CreateEntry() public method

Creates an empty entry in the Zip archive with the specified entry name. There are no restrictions on the names of entries. The last write time of the entry is set to the current time. If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name. Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)
entryName is a zero-length string. entryName is null. The ZipArchive does not support writing. The ZipArchive has already been closed.
public CreateEntry ( string entryName ) : ZipArchiveEntry
entryName string A path relative to the root of the archive, indicating the name of the entry to be created.
return ZipArchiveEntry
        public ZipArchiveEntry CreateEntry(string entryName)
        {
            Contract.Ensures(Contract.Result<ZipArchiveEntry>() != null);
            Contract.EndContractBlock();

            return DoCreateEntry(entryName, null);
        }

Same methods

ZipArchive::CreateEntry ( string entryName ) : System.IO.Compression.ZipArchiveEntry
ZipArchive::CreateEntry ( string entryName, System compressionLevel ) : System.IO.Compression.ZipArchiveEntry
ZipArchive::CreateEntry ( string entryName, CompressionLevel compressionLevel ) : ZipArchiveEntry

Usage Example

        public async Task <MemoryStream> GenerateZip(string report, string log)
        {
            using var ms      = new MemoryStream();
            using var archive =
                      new System.IO.Compression.ZipArchive(ms, ZipArchiveMode.Create, true);
            byte[] reportBytes = Encoding.ASCII.GetBytes(report);
            byte[] logBytes    = Encoding.ASCII.GetBytes(log);

            var zipEntry = archive.CreateEntry("Report.trx",
                                               CompressionLevel.Fastest);

            using (var zipStream = zipEntry.Open())
            {
                await zipStream.WriteAsync(reportBytes, 0, reportBytes.Length).ConfigureAwait(false);
            }

            var zipEntry2 = archive.CreateEntry("log.txt",
                                                CompressionLevel.Fastest);

            using (var zipStream = zipEntry2.Open())
            {
                await zipStream.WriteAsync(logBytes, 0, logBytes.Length).ConfigureAwait(false);
            }
            return(ms);
        }
All Usage Examples Of System.IO.Compression.ZipArchive::CreateEntry