/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.zip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import java.util.zip.ZipException;
/**
* Reimplementation of {@link java.util.zip.ZipOutputStream
* java.util.zip.ZipOutputStream} that does handle the extended
* functionality of this package, especially internal/external file
* attributes and extra fields with different layouts for local file
* data and central directory entries.
*
* <p>This class will try to use {@link java.io.RandomAccessFile
* RandomAccessFile} when you know that the output is going to go to a
* file.</p>
*
* <p>If RandomAccessFile cannot be used, this implementation will use
* a Data Descriptor to store size and CRC information for {@link
* #DEFLATED DEFLATED} entries, this means, you don't need to
* calculate them yourself. Unfortunately this is not possible for
* the {@link #STORED STORED} method, here setting the CRC and
* uncompressed size information is required before {@link
* #putNextEntry putNextEntry} can be called.</p>
*
*/
public class ZipOutputStream extends FilterOutputStream {
/**
* Compression method for deflated entries.
*
* @since 1.1
*/
public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED;
/**
* Default compression level for deflated entries.
*
* @since Ant 1.7
*/
public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION;
/**
* Compression method for stored entries.
*
* @since 1.1
*/
public static final int STORED = java.util.zip.ZipEntry.STORED;
/**
* Current entry.
*
* @since 1.1
*/
private ZipEntry entry;
/**
* The file comment.
*
* @since 1.1
*/
private String comment = "";
/**
* Compression level for next entry.
*
* @since 1.1
*/
private int level = DEFAULT_COMPRESSION;
/**
* Has the compression level changed when compared to the last
* entry?
*
* @since 1.5
*/
private boolean hasCompressionLevelChanged = false;
/**
* Default compression method for next entry.
*
* @since 1.1
*/
private int method = java.util.zip.ZipEntry.DEFLATED;
/**
* List of ZipEntries written so far.
*
* @since 1.1
*/
private Vector entries = new Vector();
/**
* CRC instance to avoid parsing DEFLATED data twice.
*
* @since 1.1
*/
private CRC32 crc = new CRC32();
/**
* Count the bytes written to out.
*
* @since 1.1
*/
private long written = 0;
/**
* Data for local header data
*
* @since 1.1
*/
private long dataStart = 0;
/**
* Offset for CRC entry in the local file header data for the
* current entry starts here.
*
* @since 1.15
*/
private long localDataStart = 0;
/**
* Start of central directory.
*
* @since 1.1
*/
private long cdOffset = 0;
/**
* Length of central directory.
*
* @since 1.1
*/
private long cdLength = 0;
/**
* Helper, a 0 as ZipShort.
*
* @since 1.1
*/
private static final byte[] ZERO = {0, 0};
/**
* Helper, a 0 as ZipLong.
*
* @since 1.1
*/
private static final byte[] LZERO = {0, 0, 0, 0};
/**
* Holds the offsets of the LFH starts for each entry.
*
* @since 1.1
*/
private Hashtable offsets = new Hashtable();
/**
* The encoding to use for filenames and the file comment.
*
* <p>For a list of possible values see <a
* href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>.
* Defaults to the platform's default character encoding.</p>
*
* @since 1.3
*/
private String encoding = null;
// CheckStyle:VisibilityModifier OFF - bc
/**
* This Deflater object is used for output.
*
* <p>This attribute is only protected to provide a level of API
* backwards compatibility. This class used to extend {@link
* java.util.zip.DeflaterOutputStream DeflaterOutputStream} up to
* Revision 1.13.</p>
*
* @since 1.14
*/
protected Deflater def = new Deflater(level, true);
/**
* This buffer servers as a Deflater.
*
* <p>This attribute is only protected to provide a level of API
* backwards compatibility. This class used to extend {@link
* java.util.zip.DeflaterOutputStream DeflaterOutputStream} up to
* Revision 1.13.</p>
*
* @since 1.14
*/
protected byte[] buf = new byte[512];
// CheckStyle:VisibilityModifier ON
/**
* Optional random access output.
*
* @since 1.14
*/
private RandomAccessFile raf = null;
/**
* Creates a new ZIP OutputStream filtering the underlying stream.
* @param out the outputstream to zip
* @since 1.1
*/
public ZipOutputStream(OutputStream out) {
super(out);
}
/**
* Creates a new ZIP OutputStream writing to a File. Will use
* random access if possible.
* @param file the file to zip to
* @since 1.14
* @throws IOException on error
*/
public ZipOutputStream(File file) throws IOException {
super(null);
try {
raf = new RandomAccessFile(file, "rw");
raf.setLength(0);
} catch (IOException e) {
if (raf != null) {
try {
raf.close();
} catch (IOException inner) {
// ignore
}
raf = null;
}
out = new FileOutputStream(file);
}
}
/**
* This method indicates whether this archive is writing to a seekable stream (i.e., to a random
* access file).
*
* <p>For seekable streams, you don't need to calculate the CRC or
* uncompressed size for {@link #STORED} entries before
* invoking {@link #putNextEntry}.
* @return true if seekable
* @since 1.17
*/
public boolean isSeekable() {
return raf != null;
}
/**
* The encoding to use for filenames and the file comment.
*
* <p>For a list of possible values see <a
* href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>.
* Defaults to the platform's default character encoding.</p>
* @param encoding the encoding value
* @since 1.3
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* The encoding to use for filenames and the file comment.
*
* @return null if using the platform's default character
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
这个名是名离自ant原码的,由于java.util.zip处理中文文件名时有问题可以用这个包代替那个使用就不会有中文问题了,我加了一个ZipTool.java,这个就是一个示例文件和方法打包文件。<br><br>特点:<br>一、自有原码不依赖ant.jar<br>二、支持中文文件名(压缩和解压都支持)<br>三、支持对目录压缩<br>
资源推荐
资源详情
资源评论
收起资源包目录
org.rar (12个子文件)
org
apache
tools
zip
ExtraFieldUtils.java 6KB
ZipOutputStream.java 25KB
UnixStat.java 2KB
ZipShort.java 4KB
ZipFile.java 19KB
ZipLong.java 4KB
UnrecognizedExtraField.java 4KB
ZipTool.java 11KB
ZipEntry.java 11KB
AsiExtraField.java 9KB
JarMarker.java 3KB
ZipExtraField.java 3KB
共 12 条
- 1
资源评论
- pinkmoon2012-12-03依然存在Linux文件名乱码问题,判断不同的系统修改编码即可,
- 王-鑫2013-01-17还行吧,解决了部分问题~
- Gosi_2012-10-18依然存在Linux文件名乱码问题,判断不同的系统修改编码即可,
- f11055856942014-02-16和直接用ant包是一样的
- drqdrqdr2013-05-17判断不同的系统修改编码即可
冷月宫主
- 粉丝: 1185
- 资源: 364
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功