主要用到了apach commons里的compress包。下载:http://commons.apache.org/compress/download_compress.cgi
实现了打包和解压缩的功能。
1.打包功能:主要是用了递归的方法。如果打包文件的话,则给这个文件外加一个文件夹,这里用zip文件名当一个文件夹,再进行打包。和xp打包文件然后解压的效果差不多。文章用了IOUtils.copy其实就流的读写,只不过这样代码稍微简单的。
2.解压:最主要的是要判断压缩包里的东西是文件,还是文件夹,分开来处理就可以了。这个只能解一层,如果有需要可以递归下去继续解压缩。代码如下,请指点。package zip;
import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Enumeration; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; public class CompressUtils { private OutputStream out = null; private BufferedOutputStream bos = null; private ZipArchiveOutputStream zaos = null; public static void main(String[] args) throws FileNotFoundException, Exception { CompressUtils compressUtils = new CompressUtils(); compressUtils.zip("c:/abc.zip", "d:/7767test.xls", ""); // compressUtils.unzip("c:/abc.zip", "d:/a/g/"); } public void close() throws Exception { zaos.flush(); zaos.close(); bos.flush(); bos.close(); out.flush(); out.close(); } /** * * @param zipPath * 得到的zip文件的名称(含路径) * @param filePath * 需要压缩的文件所在的目录 * @param pathName * 压缩到pathName文件夹下 * @throws Exception */ public void zip(String zipPath, String filePath, String pathName) throws Exception { File f = new File(zipPath); out = new FileOutputStream(f); bos = new BufferedOutputStream(out); zaos = new ZipArchiveOutputStream(bos); zaos.setEncoding("GBK"); if (!"".equals(pathName) && null != pathName) { pathName = pathName + File.separator; } else { pathName = f.getName().substring(0, f.getName().length() - 4) + File.separator; } zip(zaos, filePath, pathName); close(); } /** * * @param zaos * 流 * @param filePath * 需要打包的目录 * @param pathName * 打包到pathName的目录下 * @throws FileNotFoundException * @throws IOException */ public void zip(ZipArchiveOutputStream zaos, String filePath, String pathName) throws FileNotFoundException, IOException { File file2zip = new File(filePath); if (file2zip.isFile()) { zaos.putArchiveEntry(new ZipArchiveEntry(pathName + file2zip.getName())); IOUtils.copy(new FileInputStream(file2zip.getAbsolutePath()), zaos); zaos.closeArchiveEntry(); } else { File[] files = file2zip.listFiles(); if (files == null || files.length < 1) { return; } for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zip(zaos, files[i].getAbsolutePath(), pathName + files[i].getName() + File.separator); } else { zaos.putArchiveEntry(new ZipArchiveEntry(pathName + files[i].getName())); IOUtils.copy(new FileInputStream(files[i].getAbsolutePath()), zaos); zaos.closeArchiveEntry(); } } } } /** * * @param zipFileName * 压缩文件名 * @param zip2FileName * 解压路径 * @throws IOException */ public void unzip(String zipFileName, String zip2FileName) throws IOException { File zipfile = new File(zipFileName); try { zip2FileName = zip2FileName + File.separator; FileUtils.forceMkdir(new File(zip2FileName)); ZipFile zf = new ZipFile(zipfile, "GBK"); Enumeration zipArchiveEntrys = zf.getEntries(); while (zipArchiveEntrys.hasMoreElements()) { ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement(); if (zipArchiveEntry.isDirectory()) { FileUtils.forceMkdir(new File(zip2FileName + zipArchiveEntry.getName() + File.separator)); } else { IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(zip2FileName + zipArchiveEntry.getName()))); } } } catch (Exception e) { throw new IOException("找不到文件:" + zipFileName); } } }?