Öncelikle, Net Maui projesi oluşturalım ve projenin adını "encandcomp" olarak ayarlayalım.
- C# dosyası: "FileCompressor.cs"
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
public static class FileCompressor
{
public static void CompressFile(string fileToCompress, string compressedFile, byte[] key, byte[] iv)
{
using (FileStream originalFileStream = new FileStream(fileToCompress, FileMode.Open))
{
using (FileStream compressedFileStream = File.Create(compressedFile))
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// Create encryption stream
using (CryptoStream cryptoStream = new CryptoStream(compressedFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
// Create compression stream
using (GZipStream gzipStream = new GZipStream(cryptoStream, CompressionMode.Compress))
{
// Copy file to compression stream
originalFileStream.CopyTo(gzipStream);
}
}
}
}
}
}
public static void DecompressFile(string compressedFile, string decompressedFile, byte[] key, byte[] iv)
{
using (FileStream compressedFileStream = new FileStream(compressedFile, FileMode.Open))
{
using (FileStream decompressedFileStream = File.Create(decompressedFile))
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// Create decryption stream
using (CryptoStream cryptoStream = new CryptoStream(compressedFileStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
// Create decompression stream
using (GZipStream gzipStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
{
// Copy decompressed file to output stream
gzipStream.CopyTo(decompressedFileStream);
}
}
}
}
}
}
}
- XAML dosyası: "Page1.xaml"
<StackLayout>
<Label Text="Encandcomp" FontAttributes="Bold" FontSize="Large" HorizontalOptions="Center" Margin="0,30,0,0" />
<Label Text="Choose a file to compress and encrypt" FontAttributes="Bold" FontSize="Small" Margin="20,30,20,10" />
<Button Text="Select File" Clicked="SelectFileButton_Clicked" Margin="20" />
<Label Text="Selected file:" FontSize="Small" HorizontalTextAlignment="Center" Margin="0,30,0,0" />
<Label x:Name="SelectedFileLabel" FontSize="Small" HorizontalTextAlignment="Center" />
<Button Text="Compress and Encrypt" Clicked="CompressAndEncryptButton_Clicked" Margin="20" />
</StackLayout>
- C# dosyası: "Page1.xaml.cs"
using System;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Maui.Controls;
namespace encandcomp
{
public partial class Page1 : ContentPage
{
private string _selectedFilePath;
private byte[] _key;
private byte[] _iv;
public Page1()
{
InitializeComponent();
// Generate random key and iv
using (Aes aes = Aes.Create())
{
_key = aes.Key;
_iv = aes.IV;
}
}
private async void SelectFileButton_Clicked(object sender, EventArgs e)
{
await PickAndSetFile();
}
private async void CompressAndEncryptButton_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(_selectedFilePath))
{
// Get file name without extension
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_selectedFilePath);
// Create compressed file path
string compressedFilePath = Path.Combine(Path.GetDirectoryName(_selectedFilePath), $"{fileNameWithoutExtension}.zip");
// Encrypt and compress file
FileCompressor.CompressFile(_selectedFilePath, compressedFilePath, _key, _iv);
// Show success message
await DisplayAlert("Success", "File compressed and encrypted successfully.", "OK");
}
else
{
await DisplayAlert("Error", "Please select a file before compressing and encrypting.", "OK");
}
}
private async System.Threading.Tasks.Task PickAndSetFile()
{
var result = await FilePicker.PickAsync(new PickOptions
{
PickerTitle = "Pick a file"
});
if (result != null)
{
_selectedFilePath = result.FullPath;
SelectedFileLabel.Text = _selectedFilePath;
}
}
}
}
Projenin derlenmesi için aşağıdaki komutlar kullanılabilir:
-
Net Maui SDK paketlerini yükleyin (ilk kez yapılıyorsa):
dotnet new -i Microsoft.Maui.Templates
-
Proje klasörüne gidin:
cd encandcomp
-
Proje için Android uygulaması oluşturun:
dotnet new maui-android
-
Android uygulamasını derleyin:
dotnet build -t:SignAndroidPackage
-
Uygulamayı Android emülatöründe çalıştırın:
dotnet run -t:AndroidEmulator