包含javazipenty的词条
本篇文章给大家谈谈javazipenty,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中zip压缩输入输出流的问题,ZipEntry()方法里的参数到底什么意思
- 2、java中的ZipEntry是什么意思?
- 3、java 在读取压缩包时 用zipentry遍历里面的文件 比如 a/1.txt 它会读取 a和
- 4、java用apache的ZipEntry压缩文件名为中文的word文件时,文件名乱码
- 5、java 压缩比
java中zip压缩输入输出流的问题,ZipEntry()方法里的参数到底什么意思
ZipEntry 用于保存一些被压缩文件的信息,如文件名,最后访问时间,最后修改时间,创建时间,文件大小,crc 校验值 等信息。
ZipEntry 具有一个带 String 类型参数的构造方法:ZipEntry(String name), name 是入口名称,就是打开压缩文件时,看到的里面的文件名称。
可以看一下它的源码,下面是部分源码:
public
class ZipEntry implements ZipConstants, Cloneable {
String name; // entry name
long time = -1; // last modification time
FileTime mtime; // last modification time, from extra field data
FileTime atime; // last access time, from extra field data
FileTime ctime; // creation time, from extra field data
long crc = -1; // crc-32 of entry data
long size = -1; // uncompressed size of entry data
long csize = -1; // compressed size of entry data
int method = -1; // compression method
int flag = 0; // general purpose flag
byte[] extra; // optional extra field data for entry
String comment; // optional comment string for entry
...
}
java中的ZipEntry是什么意思?
一个zip文件中有若干个文件和若干个文件夹
所以我们需要判断,这个zip文件中包含的是文件还是文件夹
如:一个压缩文件myzip.zip
中有
mytext.txt(文本文件),myfile(文件夹)
那么
zipentry.isdirectory()
是用来判断myzip.zip中某一个选项是文件还是文件夹!
java 在读取压缩包时 用zipentry遍历里面的文件 比如 a/1.txt 它会读取 a和
用ZipInputStream.getNextEntry()方法遍历后的值赋给zipentry对象后。
可以取得目录及目录下的文件列表
也就是说 a 和a/1.txt都会读取的。
根据情况你可以用isDirectory来判断是不是目录来进行区分的。
java用apache的ZipEntry压缩文件名为中文的word文件时,文件名乱码
apache自带的zip方法有缺陷,没有做中文的判断的,这个是它的一个已知bug。
解决办法:用jdk的rt.jar里面的方法实现就可以了。
可以参考下以下工具类:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;
/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解压Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration? extends ZipEntry entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}
/**
* 将输入流中的数据写到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}
/**
* 安全关闭多个流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
java 压缩比
ZipOutputStream
里面有
setMethodpublic void setMethod(int method)设置用于后续条目的默认压缩方法。只要没有为单个 ZIP 文件条目指定压缩方法,并且其初始设置为 DEFLATED 时,就使用此默认值。
参数:method - 默认压缩方法
抛出:IllegalArgumentException - 如果指定的压缩方法无效
setLevelpublic void setLevel(int level)为后续的 DEFLATED 条目设置压缩级别。默认设置是 DEFAULT_COMPRESSION。
参数:level - 压缩级别 (0-9)
抛出:IllegalArgumentException - 如果压缩级别无效
关于javazipenty和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。