I wrote several methods, read the file in parts, then compress it, put everything in a queue with ready blocks and then write the blocks to a file, use data compression, and the output is a file larger than the original file, tell me why?
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.IO.Compression; namespace ConsoleApplication57 { class Program { static void Main(string[] args) { string path = @"d:\Black Widow.m4a"; string path_compres = @"d:\Compress.gz"; // создаем очередь c блоками ланных Queue<KeyValuePair<int, byte[]>> queue_block = new Queue<KeyValuePair<int, byte[]>>(); // создаем очередь с готовыми обработанными блоками Queue<KeyValuePair<int, byte[]>> readyQueue = new Queue<KeyValuePair<int, byte[]>>(); // открываем поток using (var fs = new FileStream(path, FileMode.Open)) // добавляем в очередь блоки foreach (KeyValuePair<int, byte[]> block in Read_Blockk(fs)) { queue_block.Enqueue(block); } // сжимаем и добавляем в готовую очередь while (queue_block.Count>0) { var block = queue_block.Dequeue(); var compressionBlock = COmpress(block.Key, block.Value); readyQueue.Enqueue(compressionBlock); } // пишем в файл блоки while (readyQueue.Count!=0) { Write_Final_File(path_compres,readyQueue.Dequeue().Value); } Console.ReadKey(); } public static IEnumerable<KeyValuePair<int,byte[]>> Read_Blockk(Stream stream) { const int size_block=1024 * 1024; // определяем размер буфера=1мб int index = 0; // номер блока while (stream.Position<stream.Length) { // выделяем память под массив буффера. byte[] buffer=new byte[System.Math.Min(size_block,stream.Length-stream.Position)]; stream.Read(buffer, 0, buffer.Length); yield return new KeyValuePair<int, byte[]>(index++,buffer); } } public static KeyValuePair<int, byte[]> COmpress(int index,byte[] block) { using (var ms = new MemoryStream()) using (var gzStream=new GZipStream(ms,CompressionMode.Compress)) { int ind = index; gzStream.Write(block,0,block.Length); gzStream.Close(); return new KeyValuePair<int, byte[]>(ind++,ms.ToArray()); } } public static void Write_Final_File(string path, byte[] ReadyBlock) { using (var fsWrite = new FileStream(path, FileMode.Append, FileAccess.Write)) fsWrite.Write(ReadyBlock,0,ReadyBlock.Length); } } }
rarandzip, if you squeezedrar, and say thatzipnot compressed - then this is somehow not logical.ziplittle poorer. Then take the rar-library (if you really get it) and compress it using the specified compression method (BEST). - nick_n_aCompress, if compression quality is required, you must take the appropriate tools. - NewView