如何将音频作为原始音频或字节数组写入zip存档(System.IO.Compression)?

编程入门 行业动态 更新时间:2024-10-20 04:07:09
本文介绍了如何将音频作为原始音频或字节数组写入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)?

本文发布于:2023-11-09 20:44:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:音频   数组   字节   如何将   原始

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!