如何将音频作为原始音频或字节数组写入zip存档(System.IO.Compression)? [英] How can I write audio to a zip archive (System.IO.Compression) as raw audio or as a byte array?

查看:84
本文介绍了如何将音频作为原始音频或字节数组写入zip存档(System.IO.Compression)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET WinForms应用程序,它将使用System.IO.Compression将大量不同类型的数据写入ziip存档.首先,这是zip存档过程的代码:  


       public void SaveStd2Zip(string strfilepath,List< clsStandardQuestion> lstQuestions,
              nbsp; bsp          clsProjectInfo GameInfo,List< clsMedia> lstProjMed)
       {
           clsMediaConverter MC =新的clsMediaConverter();
          使用(var ms = new MemoryStream())
           {
                            使用(var archive = new ZipArchive(ms,ZipArchiveMode.Create,true))
                             {
                    //首先,写一张图片(如果有的话)...
                   如果(GameInfo.blTSImagePresent == true)
                    {
               b var TSImage = archive.CreateEntry(" imgTSImage");

               b使用(var entryStream = TSImage.Open())
               b使用(var sw = new StreamWriter(entryStream))
               b {
              nbsp; bsp    GameInfo.imgTSImage.Save(entryStream,GameInfo.imgTSImage.RawFormat);
              nbsp; bsp    sw.Close();
               b }
                    }

                    //接下来,写音频(如果有)...
                   如果(GameInfo.blTSAudioPresent == true)
                    {
               b var TSAudio = archive.CreateEntry("audTSAudio");

               b使用(var entryStream = TSAudio.Open())
               b使用(var sw = new StreamWriter(entryStream))
               b {
              nbsp; bsp    byte [] AudioInBytes = MC.Audio2Bytes(GameInfo.audTSAudio);     //转变 音频到字节数组.
              nbsp; bsp    ms.Write(AudioInBytes,0,AudioInBytes.Length);
              nbsp; bsp   字符串strTSAudio = Convert.ToBase64String(ms.ToArray());          //将字节数组转换为Base64字符串.
              nbsp; bsp    sw.Write(strTSAudio);
              nbsp; bsp    sw.Close();
               b }
                    }
                                    }
                                   使用(var fileStream = new FileStream(strfilepath,FileMode.Create))
                                    {
              ms.Seek(0,SeekOrigin.Begin);
              ms.CopyTo(fileStream);
                                    }
                                    ms.Close();
           }
       }

到目前为止,一切正常,但是老板夫人希望我尝试一些不需要我将音频转换为Base64字符串的事情,因为Base64字符串通常是原始文件大小的1.3倍.您会注意到我将音频转换为字节数组 (byte [] AudioInBytes = MC.Audio2Bytes(GameInfo.audTSAudio).我一直坚持将字节数组转换为Base64字符串,然后将Base64字符串写入zip存档,而不是作为字节数组.字节数组直接减少 所生成的zip存档的大小超出限制?更好的方法是,有什么方法可以直接将音频直接写入存档吗?

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?

推荐答案

您好,WikiGrrrl,

Hi WikiGrrrl,

请检查以下代码,该代码使用CopyTo方法而不是转换为base64字符串.

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();

最诚挚的问候,

吴可乐


这篇关于如何将音频作为原始音频或字节数组写入zip存档(System.IO.Compression)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆