本文主要是介绍java 解压缩zip 压缩zip,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、解压缩
package zuo.file.zip.unzip;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
/*** @author * */
public class ReadZipFile {public static void main(String[] args) throws Exception {try { readZipFile("D:\\ztree.zip"); } catch (Exception e) { e.printStackTrace(); } }public static void readZipFile(String file) throws Exception { ZipFile zf = new ZipFile(file); InputStream in = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; while ((ze = zin.getNextEntry()) != null) {if (ze.isDirectory()) {System.out.println("文件夹路径:---"+ze.getName());} else { System.out.println("文件路径:---"+ze.getName());String fileName = ze.getName().substring(ze.getName().lastIndexOf("/")+1);System.out.println("文件名称:---"+fileName); //有数据if (ze.getSize() > 0) {
//现在d:盘根目录下新建一个test的空文件夹,将zip数据写进去FileUtils.copyInputStreamToFile(zf.getInputStream(ze), new File("d:\\test/"+picName));} } } zin.closeEntry(); }
}
2、压缩
package zuo.file.zip.zipandunzip;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;/*** @source http://my.oschina.net/woqi/blog/11134* @author **/
public class ZipUtil {/*** 压缩* @param src* 源文件或者目录* @param dest* 压缩文件路径* @throws IOException*/public static void zip(String src, String dest) throws IOException {ZipOutputStream out = null;try {File outFile = new File(dest);out = new ZipOutputStream(outFile);File fileOrDirectory = new File(src);if (fileOrDirectory.isFile()) {zipFileOrDirectory(out, fileOrDirectory, "");} else {File[] entries = fileOrDirectory.listFiles();for (int i = 0; i < entries.length; i++) {zipFileOrDirectory(out, entries[i], "");}}} catch (IOException ex) {ex.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException ex) {}}}}/*** 递归压缩文件或目录* * @param out* 压缩输出流对象* @param fileOrDirectory* 要压缩的文件或目录对象* @param curPath* 当前压缩条目的路径,用于指定条目名称的前缀* @throws IOException*/private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath) throws IOException {FileInputStream in = null;try {if (!fileOrDirectory.isDirectory()) {// 压缩文件byte[] buffer = new byte[4096];int bytes_read;in = new FileInputStream(fileOrDirectory);ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());out.putNextEntry(entry);while ((bytes_read = in.read(buffer)) != -1) {out.write(buffer, 0, bytes_read);}out.closeEntry();} else {// 压缩目录File[] entries = fileOrDirectory.listFiles();for (int i = 0; i < entries.length; i++) {// 递归压缩,更新curPathszipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/");}}} catch (IOException ex) {ex.printStackTrace();throw ex;} finally {if (in != null) {try {in.close();} catch (IOException ex) {}}}}public static void main(String[] args) {try {ZipUtil.zip("F:\\temp", "F:\\test.zip");} catch (Exception e) {e.printStackTrace();}}}
这篇关于java 解压缩zip 压缩zip的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!