Program.cs
                        
                             · 2.0 KiB · C#
                        
                    
                    
                      
                        Bruto
                      
                    
                      
                    
                        
                          
                        
                    
                    
                
                
                
            using System.IO.Compression;
using Newtonsoft.Json;
public class Program
{
    public static async Task Main(string[] args)
    {
        var address =
                @"qb";
        var flPath = Path.Combine(Directory.GetCurrentDirectory(), "files.txt");
        var gzPath = Path.Combine(Directory.GetCurrentDirectory(), "files.txt.gz");
        var httpClient = new HttpClient();
        var response = await httpClient.GetAsync(address);
        var content = await response.Content.ReadAsStringAsync();
        var serverResponse = JsonConvert.DeserializeObject<ServerResponse>(content)!;
        var files = serverResponse.Files!.Select(x => x.Key).ToArray();
        
        // save files to files.txt and files.txt.gz
        await File.WriteAllLinesAsync(flPath, files);
        Console.WriteLine($"Files saved to {flPath}");
        
        // save files to files.txt.gz
        await using var fileStream = File.Create(gzPath);
        await using var gzipStream = new GZipStream(fileStream, CompressionMode.Compress);
        await using var writer = new StreamWriter(gzipStream);
        foreach (var file in files)
        {
            await writer.WriteLineAsync(file);
        }
        
        Console.WriteLine($"Files saved to {gzPath}");
        
        // Read the gz file back:
        //await ReadBack(gzPath);
    }
    private static async Task ReadBack(string gzFile)
    {
        await using var fileStream = File.OpenRead(gzFile);
        await using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
        using var reader = new StreamReader(gzipStream);
        var linesArray = (await reader.ReadToEndAsync()).Split(Environment.NewLine);
        foreach (var line in linesArray)
        {
            Console.WriteLine(line);
        }
    }
}
public class ServerResponse
{
    [JsonProperty("cr:itemType")]
    public string? ItemType { get; set; }
    
    [JsonProperty("cr:files")]
    public Dictionary<string, string>? Files { get; set; }
}
                | 1 | using System.IO.Compression; | 
| 2 | using Newtonsoft.Json; | 
| 3 | |
| 4 | public class Program | 
| 5 | { | 
| 6 | public static async Task Main(string[] args) | 
| 7 | { | 
| 8 | var address = | 
| 9 | @"qb"; | 
| 10 | var flPath = Path.Combine(Directory.GetCurrentDirectory(), "files.txt"); | 
| 11 | var gzPath = Path.Combine(Directory.GetCurrentDirectory(), "files.txt.gz"); | 
| 12 | var httpClient = new HttpClient(); | 
| 13 | var response = await httpClient.GetAsync(address); | 
| 14 | var content = await response.Content.ReadAsStringAsync(); | 
| 15 | var serverResponse = JsonConvert.DeserializeObject<ServerResponse>(content)!; | 
| 16 | var files = serverResponse.Files!.Select(x => x.Key).ToArray(); | 
| 17 | |
| 18 | // save files to files.txt and files.txt.gz | 
| 19 | await File.WriteAllLinesAsync(flPath, files); | 
| 20 | Console.WriteLine($"Files saved to {flPath}"); | 
| 21 | |
| 22 | // save files to files.txt.gz | 
| 23 | await using var fileStream = File.Create(gzPath); | 
| 24 | await using var gzipStream = new GZipStream(fileStream, CompressionMode.Compress); | 
| 25 | await using var writer = new StreamWriter(gzipStream); | 
| 26 | foreach (var file in files) | 
| 27 | { | 
| 28 | await writer.WriteLineAsync(file); | 
| 29 | } | 
| 30 | |
| 31 | Console.WriteLine($"Files saved to {gzPath}"); | 
| 32 | |
| 33 | // Read the gz file back: | 
| 34 | //await ReadBack(gzPath); | 
| 35 | } | 
| 36 | |
| 37 | private static async Task ReadBack(string gzFile) | 
| 38 | { | 
| 39 | await using var fileStream = File.OpenRead(gzFile); | 
| 40 | await using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress); | 
| 41 | using var reader = new StreamReader(gzipStream); | 
| 42 | var linesArray = (await reader.ReadToEndAsync()).Split(Environment.NewLine); | 
| 43 | foreach (var line in linesArray) | 
| 44 | { | 
| 45 | Console.WriteLine(line); | 
| 46 | } | 
| 47 | } | 
| 48 | } | 
| 49 | |
| 50 | public class ServerResponse | 
| 51 | { | 
| 52 | [JsonProperty("cr:itemType")] | 
| 53 | public string? ItemType { get; set; } | 
| 54 | |
| 55 | [JsonProperty("cr:files")] | 
| 56 | public Dictionary<string, string>? Files { get; set; } | 
| 57 | } |