我使用org.apache.commons.compress 压缩zip包,用到的类有:
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
压缩的方法:
/***
* 压缩指定文件夹中的所有文件.
*
* @param zipFile
* @param folderPaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(String zipFile, String folderPaths)
throws IOException, ArchiveException {
List list = FileUtils.getListFiles(new File(folderPaths));
compressZip(zipFile, list);
}
/***
* 压缩.
*
* @param zipFile
* :压缩的结果--zip文件
* @param filePaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(File zipFile,
@SuppressWarnings("rawtypes") List filePaths) throws IOException,
ArchiveException {
FileOutputStream fou = new FileOutputStream(zipFile);
ArchiveOutputStream archOuts = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
if (archOuts instanceof ZipArchiveOutputStream) {
ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
compressZip(zipOut, filePaths);
}
}
/***
* 压缩之后,文件都是在同一级别.
*
* @param zipOut
* @param filePaths
* :要压缩的文件
* @throws IOException
*/
public static void compressZip(ZipArchiveOutputStream zipOut,
@SuppressWarnings("rawtypes") List filePaths) throws IOException {
for (Object fileObj : filePaths) {
if (fileObj instanceof File) {
File file = (File) fileObj;
addEntry(zipOut, file);
} else {
addEntry(zipOut, (String) fileObj);
}
}
closeZip(zipOut,true);
}
/***
* 与上述方法的区别就是第二个参数的类型.
*
* @param zipOut
* @param filepath
* : 要压缩的单个文件
* @throws IOException
*/
public static void addEntry(ZipArchiveOutputStream zipOut, File filepath)
throws IOException {
if (filepath.isDirectory()) {
return;
}
ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(filepath,
filepath.getName());
zipEntry2.setSize(filepath.length());
zipOut.putArchiveEntry(zipEntry2);
FileInputStream fin = new FileInputStream(filepath);
// 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
FileUtils.writeIn2Output(fin, zipOut, false, true);
}
/***
*
* @param zipOut
* @param filepath
* :要压缩的单个文件
* @throws IOException
*/
public static void addEntry(ZipArchiveOutputStream zipOut, String filepath)
throws IOException {
File singleFile = new File(filepath);
if (singleFile.isDirectory()) {
return;
}
ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(singleFile,
SystemHWUtil.getFileSimpleName(filepath));
zipEntry2.setSize(singleFile.length());
zipOut.putArchiveEntry(zipEntry2);
FileInputStream fin = new FileInputStream(filepath);
// 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
FileUtils.writeIn2Output(fin, zipOut, false, true);
}
/***
* 压缩之后的收尾操作.
*
* @param zipOut
* @throws IOException
*/
private static void closeZip(ZipArchiveOutputStream zipOut,boolean iscloseArchiveEntry)
throws IOException {
if(iscloseArchiveEntry){
zipOut.closeArchiveEntry();// it is necessary
}
zipOut.flush();
zipOut.finish();
zipOut.close();
}
但是读取使用apache compress 压缩的zip包时调用zipEntry.getSize() 返回的是-1
为什么会返回-1呢?这样我就不知道应该读取多少个字节了
6 个解决方案
#2
使用 ByteArrayOutputStream os=new ByteArrayOutputStream();
IOUtils.copy(zipIn, os, 1024); 解决问题。感谢
IOUtils.copy(zipIn, os, 1024); 解决问题。感谢
#3
zipEntry.getSize() 返回的是-1, 不对的。
#4
建议使用org.apache.commons.compress.utils.IOUtils
#5
使用org.apache.commons.compress.utils.IOUtils 绕过了zipEntry.getSize() 返回的是-1的问题
#6
#1
#2
使用 ByteArrayOutputStream os=new ByteArrayOutputStream();
IOUtils.copy(zipIn, os, 1024); 解决问题。感谢
IOUtils.copy(zipIn, os, 1024); 解决问题。感谢
#3
zipEntry.getSize() 返回的是-1, 不对的。
#4
建议使用org.apache.commons.compress.utils.IOUtils
#5
使用org.apache.commons.compress.utils.IOUtils 绕过了zipEntry.getSize() 返回的是-1的问题