¿Cómo comprimir un archivo antes de guardarlo en el disco?

I want to compress a file before saving physically on the disk.

I tried using compress and decompress methods (MSDN sample code) but all methods require a file which is already physically stored on the disk.

preguntado el 31 de enero de 12 a las 08:01

do you want save file without name? -

Can't you save file on disk and compress it and save it again, and delete the uncompressed version? Also which MSDN sample you are referring? -

4 Respuestas

The easiest way is to open the file as a Stream and wrap it with a compression API like GZipStream.

using (var fileStream = File.Open(theFilePath, FileMode.OpenOrCreate) {
  using (var stream = new GZipStream(fileStream, CompressionMode.Compress)) {
    // Write to the `stream` here and the result will be compressed
  }
}

Respondido el 31 de enero de 12 a las 12:01

@NIleshLanke then use GZipStream con CompressionMode.Decompress opción - JaredPar

The DeflateStream and GZipStream classes are best used on uncompressed sources of data. If the source data is already compressed, using these classes may actually increase the size of the stream. docs.microsoft.com/en-us/dotnet/api/… - zimba

Descripción

Puede utilizar el GZipStream class not only with a fileName. It is possible to compress a Stream.

Clase GZipStream Provides methods and properties used to compress and decompress streams.

Muestra

System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Compress);
// now you can save the file to disc

Más Información

Respondido el 31 de enero de 12 a las 12:01

Can't you use the GZipStream class? It's stream based, so you shouldn't need an on-disk file to use this class.

Which kind of data are you trying to compress?

Respondido el 31 de enero de 12 a las 12:01

Utilizan MemoryStream y la GZipStream.

File is an array of bytes so you can try following code according to http://www.dotnetperls.com/compress :

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {

            byte[] text = Encoding.ASCII.GetBytes(new string('X', 10000));
            byte[] compress = Compress(text);

            Console.WriteLine("Compressed");
            foreach (var b in compress)
            {
                Console.WriteLine("{0} ", b);
            }
            Console.ReadKey();
        }

        public static byte[] Compress(byte[] raw)
        {
            using (var memory = new MemoryStream())
            {
                using (var gzip = new GZipStream(memory, CompressionMode.Compress, true))
                {
                    gzip.Write(raw, 0, raw.Length);
                }
                return memory.ToArray();
            }
        }
    }
}

Respondido el 31 de enero de 12 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.