Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Prog_Guide.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
6.22 Mб
Скачать

Компиляция кода

Чтобы запустить этот пример, необходимо предоставить подходящую учетную запись пользователя или группы. В этом примере используется объект File; однако та же самая процедура используется для классов FileInfo, Directory и DirectoryInfo.

How to: Compress Files

Use the GZipStream class to compress and decompress data. The following code example creates a file (test.txt) in the current directory, fills it with text, and displays the contents of the file to the console. The code then uses the GZipStream class to create a compressed version of the file (test.txt.gz) and compares the size of the two files. Finally, the code reads in the compressed file, decompresses it, and writes out a new file (test.txt.gz.txt) to the current directory. The code then displays the contents of the decompressed file.

You can also use the DeflateStream class to compress and decompress data.

Example

using System;

using System.Collections.Generic;

using System.IO;

using System.IO.Compression;

public class CompressionSnippet

{

public static void Main()

{

string path = "test.txt";

// Create the text file if it doesn't already exist.

if (!File.Exists(path))

{

Console.WriteLine("Creating a new test.txt file");

string[] text = new string[] {"This is a test text file.",

"This file will be compressed and written to the disk.",

"Once the file is written, it can be decompressed",

"using various compression tools.",

"The GZipStream and DeflateStream class use the same",

"compression algorithms, the primary difference is that",

"the GZipStream class includes a cyclic redundancy check",

"that can be useful for detecting data corruption.",

"One other side note: both the GZipStream and DeflateStream",

"classes operate on streams as opposed to file-based",

"compression; data is read on a byte-by-byte basis, so it",

"is not possible to perform multiple passes to determine the",

"best compression method. Already compressed data can actually",

"increase in size if compressed with these classes."};

File.WriteAllLines(path, text);

}

Сжатие файлов

Используйте класс GZipStream для сжатия и распаковки данных. Следующий пример кода создает файл (test.txt) в текущем каталоге, заполняет его текстом и выводит содержимое файла на консоль. Затем в коде используется класс GZipStream для создания сжатой версии файла (test.txt.gz) и сравнивается размер двух файлов. В заключение в коде выполняется считывание данных из сжатого файла, его распаковка и запись нового файла (test.txt.gz.txt) в текущий каталог. Затем выводится содержимое распакованного файла.

Также можно использовать класс DeflateStream для сжатия и распаковки данных.

Пример

---------

Console.WriteLine("Contents of {0}", path);

Console.WriteLine(File.ReadAllText(path));

CompressFile(path);

Console.WriteLine();

UncompressFile(path + ".gz");

Console.WriteLine();

Console.WriteLine("Contents of {0}", path + ".gz.txt");

Console.WriteLine(File.ReadAllText(path + ".gz.txt"));

}

public static void CompressFile(string path)

{

FileStream sourceFile = File.OpenRead(path);

FileStream destinationFile = File.Create(path + ".gz");

byte[] buffer = new byte[sourceFile.Length];

sourceFile.Read(buffer, 0, buffer.Length);

using (GZipStream output = new GZipStream(destinationFile,

CompressionMode.Compress))

{

Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,

destinationFile.Name, false);

output.Write(buffer, 0, buffer.Length);

}

// Close the files.

sourceFile.Close();

destinationFile.Close();

}

public static void UncompressFile(string path)

{

FileStream sourceFile = File.OpenRead(path);

FileStream destinationFile = File.Create(path + ".txt");

// Because the uncompressed size of the file is unknown,

// we are using an arbitrary buffer size.

byte[] buffer = new byte[4096];

int n;

using (GZipStream input = new GZipStream(sourceFile,

CompressionMode.Decompress, false))

{

Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name,

destinationFile.Name);

n = input.Read(buffer, 0, buffer.Length);

destinationFile.Write(buffer, 0, n);

}

// Close the files.

sourceFile.Close();

destinationFile.Close();

}

}

------

Composing Streams

A backing store is a storage medium, such as a disk or memory. Each different backing store implements its own stream as an implementation of the Stream class. Each stream type reads and writes bytes from and to its given backing store. Streams that connect to backing stores are called base streams. Base streams have constructors that have the parameters necessary to connect the stream to the backing store. For example, FileStream has constructors that specify a path parameter, which specifies how the file will be shared by processes, and so on.

The design of the System.IO classes provides simplified stream composing. Base streams can be attached to one or more pass-through streams that provide the functionality you want. A reader or writer can be attached to the end of the chain so that the preferred types can be read or written easily.

The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. (Note that FileStreams are buffered by default.) Next, a StreamReader is created to read characters from the FileStream, which is passed to the StreamReader as its constructor argument. ReadLine reads until Peek finds no more characters.

using System;

using System.IO;

public class CompBuf

{

private const string FILE_NAME = "MyFile.txt";

public static void Main(String[] args)

{

if (!File.Exists(FILE_NAME))

{

Console.WriteLine("{0} does not exist!", FILE_NAME);

return;

}

FileStream fsIn = new FileStream(FILE_NAME, FileMode.Open,

FileAccess.Read, FileShare.Read);

// Create an instance of StreamReader that can read

// characters from the FileStream.

StreamReader sr = new StreamReader(fsIn);

// While not at the end of the file, read lines from the file.

while (sr.Peek()>-1)

{

String input = sr.ReadLine();

Console.WriteLine (input);

}

sr.Close();

}

}