Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1562 | Anzahl registrierter User: 1893 | Besucher online: 25
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Dateien mit gzip Compressen


Autor: CoReX
Sprache: C#
Bewertung:
1.44 (2 votes)
Anzahl der Aufrufe: 3257
  
Kick it on dotnet-kicks.de  

Beschreibung:

mit dieser Klasse können sie dateien mit gzip compressen/Archivieren

Abgelegt unter: gzip, gz, Compress, gz file, Compress file.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    class gz
    {
        public static void Compress(string filename)
        {
            if (filename.IndexOf(".gz") >= 1)
            {
                return;
            }
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("[GZIP]compression on file {0}", filename);
            FileStream infile;
            try
            {
                infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[infile.Length];
                int count = infile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    infile.Close();
                    Console.WriteLine("[GZIP]Failed: Unable to read data from file");
                    return;
                }
                infile.Close();
                FileStream fs = new FileStream(filename + ".gz", FileMode.Create, FileAccess.Write, FileShare.Write);
                GZipStream compressedzipStream = new GZipStream(fs, CompressionMode.Compress, true);
                Console.WriteLine("[GZIP]Compression");
                compressedzipStream.Write(buffer, 0, buffer.Length);
                compressedzipStream.Close();
                Console.WriteLine("[GZIP]Original size: {0}, Compressed size: {1}", buffer.Length, fs.Length);
                fs.Close();
                File.Delete(filename);
            }
            catch (InvalidDataException)
            {
                Console.WriteLine("[GZIP]Error: The file being read contains invalid data.");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("[GZIP]Error:The file specified was not found.");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("[GZIP]Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
            }
            catch (PathTooLongException)
            {
                Console.WriteLine("[GZIP]Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("[GZIP]Error: The specified path is invalid, such as being on an unmapped drive.");
            }
            catch (IOException)
            {
                Console.WriteLine("[GZIP]Error: An I/O error occurred while opening the file.");
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("[GZIP]Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("[GZIP]Error: You must provide parameters for GZIP.");
            }
            finally
            {
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
    }
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

hackman schrieb am:  15.11.2010 00:23:11

schlechtes snippet, weil
- schlechte fehlerbehandlung, streams und dateioperationen sollen in using block verwendet werden oder zumindest im finally block geschlossen werden
- die gzip implementierung von .net ist scheiße, die macht wohl irgendwelche zeichenumschreibungen was dazu führt, dass die komprimierte datei größer als die ursprungsdatei ist
- direkte stream kopieroperationen sind eigentlich verfügbar
Boas Enkler schrieb am:  21.11.2010 18:14:49

Vermischung von Concerncs (Consolen ausgaben) und disposes nicht beachtet.
Return in mitten einer methode statt werfen einer angebrachten Excetion... :-(


Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.