文件压缩 ZipOutputStream类
先举一个压缩单个文件的例子吧:
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
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamDemo1{
public static void main(String[] args) throws IOException{
File file = new File( "d:" + File.separator + "hello.txt" );
File zipFile = new File( "d:" + File.separator + "hello.zip" );
InputStream input = new FileInputStream(file);
ZipOutputStream zipOut = new ZipOutputStream( new FileOutputStream(
zipFile));
zipOut.putNextEntry( new ZipEntry(file.getName()));
// 设置注释
zipOut.setComment( "hello" );
int temp = 0 ;
while ((temp = input.read()) != - 1 ){
zipOut.write(temp);
}
input.close();
zipOut.close();
}
} |
【运行结果】
运行结果之前,我创建了一个hello.txt的文件,原本大小56个字节,但是压缩之后产生hello.zip之后,居然变成了175个字节,有点搞不懂。
不过结果肯定是正确的,我只是提出我的一个疑问而已。
上面的这个例子测试的是压缩单个文件,下面的们来看看如何压缩多个文件。
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
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** * 一次性压缩多个文件
* */
public class ZipOutputStreamDemo2{
public static void main(String[] args) throws IOException{
// 要被压缩的文件夹
File file = new File( "d:" + File.separator + "temp" );
File zipFile = new File( "d:" + File.separator + "zipFile.zip" );
InputStream input = null ;
ZipOutputStream zipOut = new ZipOutputStream( new FileOutputStream(
zipFile));
zipOut.setComment( "hello" );
if (file.isDirectory()){
File[] files = file.listFiles();
for ( int i = 0 ; i < files.length; ++i){
input = new FileInputStream(files[i]);
zipOut.putNextEntry( new ZipEntry(file.getName()
+ File.separator + files[i].getName()));
int temp = 0 ;
while ((temp = input.read()) != - 1 ){
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
}
} |
【运行结果】
先看看要被压缩的文件吧:
接下来看看压缩之后的:
大家自然想到,既然能压缩,自然能解压缩,在谈解压缩之前,我们会用到一个ZipFile类,先给一个这个例子吧。java中的每一个压缩文件都是可以使用ZipFile来进行表示的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
/** * ZipFile演示
* */
public class ZipFileDemo{
public static void main(String[] args) throws IOException{
File file = new File( "d:" + File.separator + "hello.zip" );
ZipFile zipFile = new ZipFile(file);
System.out.println( "压缩文件的名称为:" + zipFile.getName());
}
} |
【运行结果】:
压缩文件的名称为:d:\hello.zip
现在我们呢是时候来看看如何加压缩文件了,和之前一样,先让我们来解压单个压缩文件(也就是压缩文件中只有一个文件的情况),我们采用前面的例子产生的压缩文件hello.zip
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
|
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/** * 解压缩文件(压缩文件中只有一个文件的情况)
* */
public class ZipFileDemo2{
public static void main(String[] args) throws IOException{
File file = new File( "d:" + File.separator + "hello.zip" );
File outFile = new File( "d:" + File.separator + "unZipFile.txt" );
ZipFile zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry( "hello.txt" );
InputStream input = zipFile.getInputStream(entry);
OutputStream output = new FileOutputStream(outFile);
int temp = 0 ;
while ((temp = input.read()) != - 1 ){
output.write(temp);
}
input.close();
output.close();
}
} |
【运行结果】:
解压缩之前:
这个压缩文件还是175字节
解压之后产生:
又回到了56字节,表示郁闷。
现在让我们来解压一个压缩文件中包含多个文件的情况吧
ZipInputStream类
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
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
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/** * 解压缩一个压缩文件中包含多个文件的情况
* */
public class ZipFileDemo3{
public static void main(String[] args) throws IOException{
File file = new File( "d:" + File.separator + "zipFile.zip" );
File outFile = null ;
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream( new FileInputStream(file));
ZipEntry entry = null ;
InputStream input = null ;
OutputStream output = null ;
while ((entry = zipInput.getNextEntry()) != null ){
System.out.println( "解压缩" + entry.getName() + "文件" );
outFile = new File( "d:" + File.separator + entry.getName());
if (!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if (!outFile.exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0 ;
while ((temp = input.read()) != - 1 ){
output.write(temp);
}
input.close();
output.close();
}
}
} |
【运行结果】:
被解压的文件:
解压之后再D盘下会出现一个temp文件夹,里面内容:
PushBackInputStream回退流
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
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
/** * 回退流操作
* */
public class PushBackInputStreamDemo{
public static void main(String[] args) throws IOException{
String str = "hello,rollenholt" ;
PushbackInputStream push = null ;
ByteArrayInputStream bat = null ;
bat = new ByteArrayInputStream(str.getBytes());
push = new PushbackInputStream(bat);
int temp = 0 ;
while ((temp = push.read()) != - 1 ){
if (temp == ',' ){
push.unread(temp);
temp = push.read();
System.out.print( "(回退" + ( char ) temp + ") " );
} else {
System.out.print(( char ) temp);
}
}
}
} |
【运行结果】:
hello(回退,) rollenholt
1
2
3
4
5
6
7
8
|
/** * 取得本地的默认编码
* */
public class CharSetDemo{
public static void main(String[] args){
System.out.println( "系统默认编码为:" + System.getProperty( "file.encoding" ));
}
} |
【运行结果】:
系统默认编码为:GBK
乱码的产生:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/** * 乱码的产生
* */
public class CharSetDemo2{
public static void main(String[] args) throws IOException{
File file = new File( "d:" + File.separator + "hello.txt" );
OutputStream out = new FileOutputStream(file);
byte [] bytes = "你好" .getBytes( "ISO8859-1" );
out.write(bytes);
out.close();
}
} |
【运行结果】:
??
一般情况下产生乱码,都是由于编码不一致的问题。
转自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
相关推荐
java中的IO整理完整版
Java中IO系统总结[整理].pdf
此文档是对JAVA 中的 IO 流的整理,其中有大多实用 而平时可以接触到的 IO 基础,对开发工作者有很大的帮助
Java中的IO整理完整版
JAVA_IO流整理思维导图.
java IO全面整理,整理了一下关于java 的IO操作,我是直接在测试的时候将关键的测试代码放上去了,并配以简洁的注解,适合有一定基础的朋友!
javaIO流整理.txt
此文档属于本人当初学习java基础之IO流,所整理的文档。里面有字节流与字符流的比较,也有总结使用不同方式读取文档的demo。希望对你的学习有帮助,谢谢!
Java io系统的设计初衷,就是为了实现“文件、控制台、网络设备”这些io设置的通信。例如,对于一个文件,我们...而到了java 1.1,为了与国际化进行接轨,在java io中添加了许多以字符(Unicode)为单位进行操作的类。
自己整理了一下javaIO流的相关知识点 用xmind软件做了一下
Java IO流思维导图,主要摘录整理的是java.io.*包下的所有IO对象,其中对应备注里包含各个IO对象的构造方法
对于服务器的并发处理能力,我们需要的是:每一毫秒服务器都能及时处理这一毫秒内收到的数百个不同TCP连接上的报文,与此同时,可能服务器上还有数以十万计的最近几秒没有收发任何报文的相对不活跃连接。...
Java多线程.drawio
该文档主要整理了Java IO流的相关信息,主要包括IO流的按照不同维度的分类、节点流、处理流、输入输出流的处理过程、抽象基类的使用等细节内容
网上都是不带书签,并且有些地方翻译有歧义。 所以我整理了一个。 特点:带书签 Scalable io in java 中文版,并且对有歧义的语义进行了修改。
Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚其结构,需要明白两点: 1. 其对称性质:InputStream 与 OutputStream, Reader 与 Writer,他们分别是一套字节输入-输出,字符输入-输出体系 2. 原始处理器(适配器)...
Java线程和IO总结[整理].pdf
Java高级技术整理,包含多线程、虚拟机、JAVA IO/NIO 、Java集合 等高级进阶知识点
最新整理的Java中的IO与NIO相关面试题目总结,java求职面试题,共15题,含答案解析,希望能帮到有需求的同学
java详细的知识点整理,包括:jvm原理、IO、类加载过程、集合、线程、反射、泛型等java基础,spring原理、特点,微服务架构、数据库引擎、消息组件、算法、数据结构等。偏理论的知识较多,主要用于面试。