转自: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 |
} |
相关推荐
MiGz 是一个多线程、gzip兼容的压缩和解压缩,可作为独立于平台的Java库和命令行工具
使用java语言可以很方便的实现gzip压缩和解压缩.以下是一个压缩和解压缩的最简程序.
文件压缩是在计算机应用程序和数据存储中非常常见的任务。在这个实战博客中,我们将创建一个简单的Java文件压缩工具,...支持不同的压缩格式:我们将使用Java的压缩和解压缩库来支持不同的压缩格式,如ZIP、GZIP等。
gzip并不将文件归档,仅只是对单个文件进行压缩,所以,在UNIX平台上,命令tar通常用来创建一个档案文件,然后命令gzip来将档案文件压缩。 Java I/O类库还收录了一些能读写压缩格式流的类。要想提供压缩功能,只要...
Java文件压缩与解压缩实践是指利用Java编程语言实现对文件或文件夹进行压缩和解压缩操作。以下是该实践的描述: 技术概述: Java文件压缩与解压缩是指将一个或多个文件或文件夹打包成一个压缩文件,或者将压缩文件...
使用java语言可以很方便的实现gzip压缩和解压缩.以下是一个压缩和解压缩的简程序. (压缩:gzip.java) import java.io.*; import java.util.zip.*; public class gzip { public static void main(String[] ...
commons-compress-1.18.jar是一个Java压缩库,专注于数据的压缩和解压缩功能,支持多种常见的压缩格式。它提供了广泛的压缩算法和工具,用于数据存储、传输和优化存储空间。 【使用人群】 适用于Java开发者、数据...
本文主要介绍java压缩和解压缩Zip、Jar、Gzip文件的知识,这里整理了相关资料,并附示例代码有兴趣的小伙伴可以参考下
gzip压缩源代码,由java语言编写,里面有个ZipUtil类,其中有两个静态方法可以方便调用用来压缩和解压缩。该包可以用于j2me和android程序中。
拉链 特征 创建并提取以下归档类型: 压缩 罐 Tarball(GZIP) Tarball(BZIP2) 泰伯(LZMA) 泰伯(XZ) 压缩和解压缩GZIP ...对于压缩和解压缩,此应用程序使用Apache Foundation的commons-compres
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装 openssl openssl-devel openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔 解压 tar -zxvf nginx-...
修正附件下载时被gzip压缩的错误 修正了父板块不统计子板块发帖数的缺陷 对子板块级数进行限制,只能有两级,防止串版 修正调整板块顺序需要执行多次才能生效的缺陷 修正所有ReturnURL返回时页面报错的历史遗留...