/*
* @(#)CNZipOutputStream.java 1.27 03/02/07
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util.zip;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* This class implements an output stream filter for writing files in the
* ZIP file format. Includes support for both compressed and uncompressed
* entries.
*
* @author David Connelly
* @version 1.27, 02/07/03
*/
public
class CNZipOutputStream extends DeflaterOutputStream implements ZipConstants {
private ZipEntry entry;
private Vector entries = new Vector();
private Hashtable names = new Hashtable();
private CRC32 crc = new CRC32();
private long written;
private long locoff = 0;
private String comment;
private int method = DEFLATED;
private boolean finished;
private String encoding="UTF-8";
private boolean closed = false;
/**
* Check to make sure that this stream has not been closed
*/
private void ensureOpen() throws IOException {
if (closed) {
throw new IOException("Stream closed");
}
}
/**
* Compression method for uncompressed (STORED) entries.
*/
public static final int STORED = ZipEntry.STORED;
/**
* Compression method for compressed (DEFLATED) entries.
*/
public static final int DEFLATED = ZipEntry.DEFLATED;
/**
* Creates a new ZIP output stream.
* @param out the actual output stream
*/
public CNZipOutputStream(OutputStream out) {
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
usesDefaultDeflater = true;
}
public CNZipOutputStream(OutputStream out,String encoding) {
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
usesDefaultDeflater = true;
this.encoding=encoding;
}
/**
* Sets the ZIP file comment.
* @param comment the comment string
* @exception IllegalArgumentException if the length of the specified
* ZIP file comment is greater than 0xFFFF bytes
*/
public void setComment(String comment) {
if (comment != null && comment.length() > 0xffff/3
&& getUTF8Length(comment) > 0xffff) {
throw new IllegalArgumentException("ZIP file comment too long.");
}
this.comment = comment;
}
/**
* Sets the default compression method for subsequent entries. This
* default will be used whenever the compression method is not specified
* for an individual ZIP file entry, and is initially set to DEFLATED.
* @param method the default compression method
* @exception IllegalArgumentException if the specified compression method
* is invalid
*/
public void setMethod(int method) {
if (method != DEFLATED && method != STORED) {
throw new IllegalArgumentException("invalid compression method");
}
this.method = method;
}
/**
* Sets the compression level for subsequent entries which are DEFLATED.
* The default setting is DEFAULT_COMPRESSION.
* @param level the compression level (0-9)
* @exception IllegalArgumentException if the compression level is invalid
*/
public void setLevel(int level) {
def.setLevel(level);
}
/**
* Begins writing a new ZIP file entry and positions the stream to the
* start of the entry data. Closes the current entry if still active.
* The default compression method will be used if no compression method
* was specified for the entry, and the current time will be used if
* the entry has no set modification time.
* @param e the ZIP entry to be written
* @exception ZipException if a ZIP format error has occurred
* @exception IOException if an I/O error has occurred
*/
public void putNextEntry(ZipEntry e) throws IOException {
ensureOpen();
if (entry != null) {
closeEntry(); // close previous entry
}
if (e.time == -1) {
e.setTime(System.currentTimeMillis());
}
if (e.method == -1) {
e.method = method; // use default method
}
switch (e.method) {
case DEFLATED:
if (e.size == -1 || e.csize == -1 || e.crc == -1) {
// store size, compressed size, and crc-32 in data descriptor
// immediately following the compressed entry data
e.flag = 8;
} else if (e.size != -1 && e.csize != -1 && e.crc != -1) {
// store size, compressed size, and crc-32 in LOC header
e.flag = 0;
} else {
throw new ZipException(
"DEFLATED entry missing size, compressed size, or crc-32");
}
e.version = 20;
break;
case STORED:
// compressed size, uncompressed size, and crc-32 must all be
// set for entries using STORED compression method
if (e.size == -1) {
e.size = e.csize;
} else if (e.csize == -1) {
e.csize = e.size;
} else if (e.size != e.csize) {
throw new ZipException(
"STORED entry where compressed != uncompressed size");
}
if (e.size == -1 || e.crc == -1) {
throw new ZipException(
"STORED entry missing size, compressed size, or crc-32");
}
e.version = 10;
e.flag = 0;
break;
default:
throw new ZipException("unsupported compression method");
}
e.offset = written;
if (names.put(e.name, e) != null) {
throw new ZipException("duplicate entry: " + e.name);
}
writeLOC(e);
entries.addElement(e);
entry = e;
}
/**
* Closes the current ZIP entry and positions the stream for writing
* the next entry.
* @exception ZipException if a ZIP format error has occurred
* @exception IOException if an I/O error has occurred
*/
public void closeEntry() throws IOException {
ensureOpen();
ZipEntry e = entry;
if (e != null) {
switch (e.method) {
case DEFLATED:
def.finish();
while (!def.finished()) {
deflate();
}
if ((e.flag & 8) == 0) {
// verify size, compressed size, and crc-32 settings
if (e.size != def.getTotalIn()) {
throw new ZipException(
"invalid entry size (expected " + e.size +
" but got " + def.getTotalIn() + " bytes)");
}
if (e.csize != def.getTotalOut()) {
throw new ZipException(
"invalid entry compressed size (expected " +
e.csize + " but got " + def.getTotalOut() +
" bytes)");
}
if (e.crc != crc.getValue()) {
throw new ZipException(
"invalid entry CRC-32 (expected 0x" +
Long.toHexString(e.crc) + " but got 0x" +
Long.toHexString(crc.getValue()) + ")");
}
} else {
e.size = def.getTotalIn();
e.csize = def.getTotalOut();
e.crc = crc.getValue();
writeEXT(e);
}
def.reset();
written += e.csize;
break;
case STORED:
// we already know that both e.size and e.csize are the same
if (e.size != written - locoff) {
throw new ZipException(
"invalid entry size (expected " + e.size +
" but got " + (written - locoff) + " bytes)");
}
if (e.crc != crc.getValue()) {
throw new ZipException(
"invalid entry crc-32 (expected 0x" +
Long.toHexString(e.crc) + " but got 0x" +
Long.toHexString(crc.getValue()) + ")");
}
break;
default:
throw new InternalError("invalid compression method");
}
crc.reset();
entry = null;
}
}
/**
* Writes an array of bytes to the current ZIP entry data. This method
* will block until all the bytes are written.
* @param b the data to be written
* @param off the start offset in the data
* @param len the number of bytes that are written
* @exception ZipException if a ZIP file
没有合适的资源?快使用搜索试试~ 我知道了~
Java实用程序设计100例(源代码)
共520个文件
class:192个
java:116个
bmp:114个
需积分: 3 57 下载量 67 浏览量
2008-01-07
13:00:22
上传
评论 1
收藏 3.63MB RAR 举报
温馨提示
java应用学习,从例子中加深java印象
资源推荐
资源详情
资源评论
收起资源包目录
Java实用程序设计100例(源代码) (520个子文件)
java.policy.applet 141B
89-2.bmp 946KB
87-1.bmp 569KB
87-2.bmp 569KB
94-1.bmp 538KB
28-2.bmp 525KB
1.bmp 522KB
1.bmp 517KB
27-2.bmp 507KB
89-3.bmp 505KB
80-1.bmp 496KB
43-1.bmp 464KB
80-2.bmp 447KB
80-3.bmp 447KB
96-2.bmp 446KB
87-3.bmp 420KB
89-1.bmp 404KB
23-2.bmp 401KB
23-1.bmp 401KB
73-1.bmp 379KB
73-1.bmp 379KB
86-1.bmp 370KB
53-1.bmp 352KB
76-1.bmp 350KB
97-1.bmp 349KB
45-1.bmp 344KB
77-1.bmp 334KB
95-1.bmp 326KB
47-1.bmp 316KB
95-2.bmp 314KB
103-1.bmp 304KB
100-1.bmp 300KB
101-1.bmp 298KB
102-1.bmp 298KB
104-1.bmp 297KB
96-1.bmp 293KB
102-2.bmp 293KB
30-1.bmp 288KB
42-1.bmp 277KB
41-1.bmp 264KB
41-2.bmp 264KB
48-2.bmp 264KB
48-1.bmp 264KB
7-1.bmp 262KB
98-1.bmp 259KB
61-1.bmp 257KB
35-2.bmp 254KB
35-1.bmp 254KB
62-1.bmp 252KB
99-1.bmp 246KB
37-1.bmp 238KB
60-1.bmp 237KB
34-2.bmp 237KB
34-1.bmp 237KB
9-2.bmp 229KB
39-1.bmp 215KB
38-1.bmp 214KB
44-1.bmp 212KB
14-1.bmp 195KB
18-1.bmp 194KB
63-2.bmp 191KB
52-3.bmp 189KB
52-2.bmp 189KB
52-1.bmp 189KB
92-1.bmp 188KB
40-1.bmp 185KB
88-1.bmp 184KB
49-1.bmp 184KB
83-1.bmp 176KB
54-1.bmp 176KB
13-1.bmp 176KB
21-1.bmp 176KB
64-1.bmp 176KB
65-1.bmp 176KB
10-1.bmp 171KB
11-1.bmp 171KB
55-1.bmp 171KB
56-1.bmp 171KB
19-1.bmp 169KB
8-1.bmp 164KB
31-1.bmp 163KB
33-1.bmp 159KB
58-1.bmp 154KB
50-2.bmp 152KB
50-1.bmp 152KB
66-1.bmp 150KB
32-1.bmp 149KB
17-1.bmp 147KB
27-1.bmp 147KB
63-1.bmp 145KB
59-1.bmp 145KB
29-1.bmp 141KB
36-2.bmp 140KB
36-1.bmp 140KB
26-1.bmp 139KB
20-1.bmp 132KB
25-1.bmp 129KB
15-2.bmp 126KB
15-1.bmp 126KB
15-3.bmp 126KB
共 520 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
收获de季节
- 粉丝: 14
- 资源: 55
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Blazor 下的 Json 编辑器
- q6.zip
- 【消息队列 】面试题.pdf
- Dell EMC Unity-Misc Procedures- Service Commands-3.pdf
- MiniSMB-HurricaneII
- 软专2302赵炳坤2301990241.ste
- 缓存面试题大全 pdf版
- SC Series-SC5020 Replacement- Battery Backup Unit-1.pdf
- SC Series-SC5020 Replacement-Choose an Option- Hard Drives-1.pdf
- 洛雪音源示例模板lx-music-source-example
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功