转自:http://www.oschina.net/code/snippet_12_259
. [代码]CompressFileGZIP.java
01 | import java.util.zip.GZIPOutputStream; |
02 | import java.io.FileOutputStream; |
03 | import java.io.FileInputStream; |
04 | import java.io.FileNotFoundException; |
05 | import java.io.IOException; |
06 |
07 | /** |
08 | * ----------------------------------------------------------------------------- |
09 | * Used to provide an example of compressing a file using the GZIP Format. |
10 | * |
11 | * @version 1.0 |
12 | * @author Jeffrey M. Hunter (jhunter@idevelopment.info) |
13 | * @author http://www.idevelopment.info |
14 | * ----------------------------------------------------------------------------- |
15 | */ |
16 |
17 | public class CompressFileGZIP { |
18 |
19 | /** |
20 | * Perform file compression. |
21 | * @param inFileName Name of the file to be compressed |
22 | */ |
23 | private static void doCompressFile(String inFileName) { |
24 |
25 | try { |
26 | |
27 | System.out.println( "Creating the GZIP output stream." ); |
28 | String outFileName = inFileName + ".gz" ; |
29 | GZIPOutputStream out = null ; |
30 | try { |
31 | out = new GZIPOutputStream( new FileOutputStream(outFileName)); |
32 | } catch (FileNotFoundException e) { |
33 | System.err.println( "Could not create file: " + outFileName); |
34 | System.exit( 1 ); |
35 | } |
36 | |
37 |
38 | System.out.println( "Opening the input file." ); |
39 | FileInputStream in = null ; |
40 | try { |
41 | in = new FileInputStream(inFileName); |
42 | } catch (FileNotFoundException e) { |
43 | System.err.println( "File not found. " + inFileName); |
44 | System.exit( 1 ); |
45 | } |
46 |
47 | System.out.println( "Transfering bytes from input file to GZIP Format." ); |
48 | byte [] buf = new byte [ 1024 ]; |
49 | int len; |
50 | while ((len = in.read(buf)) > 0 ) { |
51 | out.write(buf, 0 , len); |
52 | } |
53 | in.close(); |
54 |
55 | System.out.println( "Completing the GZIP file" ); |
56 | out.finish(); |
57 | out.close(); |
58 | |
59 | } catch (IOException e) { |
60 | e.printStackTrace(); |
61 | System.exit( 1 ); |
62 | } |
63 |
64 | } |
65 |
66 | /** |
67 | * Sole entry point to the class and application. |
68 | * @param args Array of String arguments. |
69 | */ |
70 | public static void main(String[] args) { |
71 | |
72 | if (args.length != 1 ) { |
73 | System.err.println( "Usage: java CompressFileGZIP filename" ); |
74 | } else { |
75 | doCompressFile(args[ 0 ]); |
76 | } |
77 |
78 | |
79 | } |
80 |
81 | } |
2. [代码]UncompressFileGZIP.java
001 | import java.util.zip.GZIPInputStream; |
002 | import java.io.FileOutputStream; |
003 | import java.io.FileInputStream; |
004 | import java.io.FileNotFoundException; |
005 | import java.io.IOException; |
006 |
007 | /** |
008 | * ----------------------------------------------------------------------------- |
009 | * Used to provide an example of uncompressing a file in the GZIP Format. |
010 | * |
011 | * @version 1.0 |
012 | * @author Jeffrey M. Hunter (jhunter@idevelopment.info) |
013 | * @author http://www.idevelopment.info |
014 | * ----------------------------------------------------------------------------- |
015 | */ |
016 |
017 | public class UncompressFileGZIP { |
018 |
019 | /** |
020 | * Uncompress the incoming file. |
021 | * @param inFileName Name of the file to be uncompressed |
022 | */ |
023 | private static void doUncompressFile(String inFileName) { |
024 |
025 | try { |
026 |
027 | if (!getExtension(inFileName).equalsIgnoreCase( "gz" )) { |
028 | System.err.println( "File name must have extension of \".gz\"" ); |
029 | System.exit( 1 ); |
030 | } |
031 |
032 | System.out.println( "Opening the compressed file." ); |
033 | GZIPInputStream in = null ; |
034 | try { |
035 | in = new GZIPInputStream( new FileInputStream(inFileName)); |
036 | } catch (FileNotFoundException e) { |
037 | System.err.println( "File not found. " + inFileName); |
038 | System.exit( 1 ); |
039 | } |
040 |
041 | System.out.println( "Open the output file." ); |
042 | String outFileName = getFileName(inFileName); |
043 | FileOutputStream out = null ; |
044 | try { |
045 | out = new FileOutputStream(outFileName); |
046 | } catch (FileNotFoundException e) { |
047 | System.err.println( "Could not write to file. " + outFileName); |
048 | System.exit( 1 ); |
049 | } |
050 |
051 | System.out.println( "Transfering bytes from compressed file to the output file." ); |
052 | byte [] buf = new byte [ 1024 ]; |
053 | int len; |
054 | while ((len = in.read(buf)) > 0 ) { |
055 | out.write(buf, 0 , len); |
056 | } |
057 |
058 | System.out.println( "Closing the file and stream" ); |
059 | in.close(); |
060 | out.close(); |
061 | |
062 | } catch (IOException e) { |
063 | e.printStackTrace(); |
064 | System.exit( 1 ); |
065 | } |
066 |
067 | } |
068 |
069 | /** |
070 | * Used to extract and return the extension of a given file. |
071 | * @param f Incoming file to get the extension of |
072 | * @return <code>String</code> representing the extension of the incoming |
073 | * file. |
074 | */ |
075 | public static String getExtension(String f) { |
076 | String ext = "" ; |
077 | int i = f.lastIndexOf( '.' ); |
078 |
079 | if (i > 0 && i < f.length() - 1 ) { |
080 | ext = f.substring(i+ 1 ); |
081 | } |
082 | return ext; |
083 | } |
084 |
085 | /** |
086 | * Used to extract the filename without its extension. |
087 | * @param f Incoming file to get the filename |
088 | * @return <code>String</code> representing the filename without its |
089 | * extension. |
090 | */ |
091 | public static String getFileName(String f) { |
092 | String fname = "" ; |
093 | int i = f.lastIndexOf( '.' ); |
094 |
095 | if (i > 0 && i < f.length() - 1 ) { |
096 | fname = f.substring( 0 ,i); |
097 | } |
098 | return fname; |
099 | } |
100 |
101 | /** |
102 | * Sole entry point to the class and application. |
103 | * @param args Array of String arguments. |
104 | */ |
105 | public static void main(String[] args) { |
106 | |
107 | if (args.length != 1 ) { |
108 | System.err.println( "Usage: java UncompressFileGZIP gzipfile" ); |
109 | } else { |
110 | doUncompressFile(args[ 0 ]); |
111 | } |
112 |
113 | } |
114 |
115 | } |