none
How can I write audio to a zip archive (System.IO.Compression) as raw audio or as a byte array? RRS feed

  • Question

  • I'm working on a C# .NET WinForms app which will write a bunch of different kinds of data to a ziip archive using System.IO.Compression. First, here's the code for the zip archive process:   


            public void SaveStd2Zip(string strfilepath, List<clsStandardQuestion> lstQuestions,
                                       clsProjectInfo GameInfo, List<clsMedia> lstProjMed)
            {
                clsMediaConverter MC = new clsMediaConverter();
                using (var ms = new MemoryStream())
                {
                    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                    {
                        // First, write the image if present...
                        if (GameInfo.blTSImagePresent == true)
                        {
                            var TSImage = archive.CreateEntry("imgTSImage");

                            using (var entryStream = TSImage.Open())
                            using (var sw = new StreamWriter(entryStream))
                            {
                                GameInfo.imgTSImage.Save(entryStream, GameInfo.imgTSImage.RawFormat);
                                sw.Close();
                            }
                        }

                        // Next, write the audio if present...
                        if (GameInfo.blTSAudioPresent == true)
                        {
                            var TSAudio = archive.CreateEntry("audTSAudio");

                            using (var entryStream = TSAudio.Open())
                            using (var sw = new StreamWriter(entryStream))
                            {
                                byte[] AudioInBytes = MC.Audio2Bytes(GameInfo.audTSAudio);     // Convert audio to a byte array.
                                ms.Write(AudioInBytes, 0, AudioInBytes.Length);
                                string strTSAudio = Convert.ToBase64String(ms.ToArray());           // Convert byte array to a Base64 string.
                                sw.Write(strTSAudio);
                                sw.Close();
                            }
                        }
                     }
                     using (var fileStream = new FileStream(strfilepath, FileMode.Create))
                     {
                         ms.Seek(0, SeekOrigin.Begin);
                         ms.CopyTo(fileStream);
                     }
                     ms.Close();
                }
            }

    So far, everything works, but the boss lady would like me to try something that doesn't require me to convert audio to a Base64 string, since a Base64 strings is usually 1.3 times the size of the original file. You'll notice I convert the audio to a byte array (byte[]AudioInBytes = MC.Audio2Bytes(GameInfo.audTSAudio). I'm stuck with converting the byte array to a Base64 string and writing the Base64 string to the zip archive instead of as a byte array. Any way I can write that byte array directly and reduce the over size of the resulting zip archive? Better yet, is there a way to just write the audio directly to the archive?
    • Edited by WikiGrrrl Saturday, June 10, 2017 11:05 PM typos
    Saturday, June 10, 2017 7:28 PM

All replies

  • Hi WikiGrrrl,

    Please check the following code, which use CopyTo method instead of convert to base64 string.

    string strfilepath = @"D:\Data\test.zip";
                string audioFile = @"D:\Data\test.wav";
    
                byte[] arrayFile = File.ReadAllBytes(audioFile);
    
                using (var ms = new MemoryStream())
                {
                    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                    {
                        var TSAudio = archive.CreateEntry("audTSAudio");
                        using (var originalFileStream = new MemoryStream(arrayFile))
                        {
                            using (var zipEntryStream = TSAudio.Open())
                            {
                                //Copy the attachment stream to the zip entry stream
                                originalFileStream.CopyTo(zipEntryStream);
                            }
                        }
                    }
                    using (var fileStream = new FileStream(strfilepath, FileMode.Create))
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                        ms.CopyTo(fileStream);
                    }
                    ms.Close();
                }

    If you know the file path, please try the following code.

    string strfilepath = @"D:\Data\test.zip";
    string audioFile = @"D:\Data\test.wav";
    FileInfo fileInfo = new FileInfo(audioFile);
    ZipArchive zip = ZipFile.Open(strfilepath, ZipArchiveMode.Create);
    zip.CreateEntryFromFile(audioFile, Path.GetFileName(audioFile), CompressionLevel.Optimal);
                zip.Dispose();

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 12, 2017 5:37 AM
    Moderator