using System;
using System.Data.SQLite;
using System.IO;
using System.IO.Compression;
public class FileCompressionExample
{
public static void CompressFile(string sourceFile, string destinationFile)
{
using (FileStream originalFileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
{
using (FileStream compressedFileStream = File.Create(destinationFile))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
}
}
public static void DecompressFile(string sourceFile, string destinationFile)
{
using (FileStream compressedFileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
{
using (GZipStream decompressionStream = new GZipStream(compressedFileStream, CompressionMode.Decompress))
{
using (FileStream originalFileStream = File.Create(destinationFile))
{
decompressionStream.CopyTo(originalFileStream);
}
}
}
}
public static void StoreDataInSQLite(string filePath, byte[] data)
{
using (SQLiteConnection connection = new SQLiteConnection($"Data Source={filePath}"))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = "INSERT INTO Files (Data) VALUES (@data)";
command.Parameters.AddWithValue("@data", data);
command.ExecuteNonQuery();
}
}
}
public static byte[] RetrieveDataFromSQLite(string filePath)
{
using (SQLiteConnection connection = new SQLiteConnection($"Data Source={filePath}"))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = "SELECT Data FROM Files LIMIT 1";
using (SQLiteDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return
评论已关闭