1- // SharpZipLibrary samples
1+ // SharpZipLib samples
22// Copyright © 2000-2016 AlphaSierraPapa for the SharpZipLib Team
33// All rights reserved.
44//
3232
3333class MainClass
3434{
35-
35+
3636 public static void Main ( string [ ] args )
3737 {
3838 // Perform some simple parameter checking. More could be done
@@ -53,32 +53,32 @@ public static void Main(string[] args)
5353 // Depending on the directory this could be very large and would require more attention
5454 // in a commercial package.
5555 string [ ] filenames = Directory . GetFiles ( args [ 0 ] ) ;
56-
56+
5757 // 'using' statements guarantee the stream is closed properly which is a big source
5858 // of problems otherwise. Its exception safe as well which is great.
5959 using ( ZipOutputStream s = new ZipOutputStream ( File . Create ( args [ 1 ] ) ) ) {
60-
60+
6161 s . SetLevel ( 9 ) ; // 0 - store only to 9 - means best compression
62-
62+
6363 byte [ ] buffer = new byte [ 4096 ] ;
64-
64+
6565 foreach ( string file in filenames ) {
66-
66+
6767 // Using GetFileName makes the result compatible with XP
6868 // as the resulting path is not absolute.
6969 ZipEntry entry = new ZipEntry ( Path . GetFileName ( file ) ) ;
70-
70+
7171 // Setup the entry data as required.
72-
72+
7373 // Crc and size are handled by the library for seakable streams
7474 // so no need to do them here.
7575
7676 // Could also use the last write time or similar for the file.
7777 entry . DateTime = DateTime . Now ;
7878 s . PutNextEntry ( entry ) ;
79-
79+
8080 using ( FileStream fs = File . OpenRead ( file ) ) {
81-
81+
8282 // Using a fixed size buffer here makes no noticeable difference for output
8383 // but keeps a lid on memory usage.
8484 int sourceBytes ;
@@ -88,21 +88,21 @@ public static void Main(string[] args)
8888 } while ( sourceBytes > 0 ) ;
8989 }
9090 }
91-
91+
9292 // Finish/Close arent needed strictly as the using statement does this automatically
93-
93+
9494 // Finish is important to ensure trailing information for a Zip file is appended. Without this
9595 // the created file would be invalid.
9696 s . Finish ( ) ;
97-
97+
9898 // Close is important to wrap things up and unlock the file.
9999 s . Close ( ) ;
100100 }
101101 }
102102 catch ( Exception ex )
103103 {
104104 Console . WriteLine ( "Exception during processing {0}" , ex ) ;
105-
105+
106106 // No need to rethrow the exception as for our purposes its handled.
107107 }
108108 }
0 commit comments