java file zip and upzip 博客分类: zip zipjava
程序员文章站
2024-03-24 17:02:46
...
java zip file use Apache Compress
java upzip file
private void zip(File file) throws IOException { zipFile = new File(file.getParent(), file.getName()+".zip"); FileOutputStream fOut = null; BufferedOutputStream bOut = null; ZipArchiveOutputStream tOut = null; try { fOut = new FileOutputStream(zipFile); bOut = new BufferedOutputStream(fOut); tOut = new ZipArchiveOutputStream(bOut); addFileToZip(tOut, file, ""); } finally { if(tOut!=null){ tOut.finish(); } tOut.close(); bOut.close(); fOut.close(); } } private void addFileToZip(ZipArchiveOutputStream zOut, File file, String base) throws IOException { String entryName = base + file.getName(); ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, entryName); zOut.putArchiveEntry(zipEntry); if (file.isFile()) { FileInputStream fInputStream = null; try { fInputStream = new FileInputStream(file); IOUtils.copy(fInputStream, zOut); zOut.closeArchiveEntry(); } finally { IOUtils.closeQuietly(fInputStream); } } else { zOut.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) { addFileToZip(zOut, child, entryName + "/"); } } } }
java upzip file
private void unZipFile(File fSourceZip) throws ZipException, IOException { long fileSize=0; String zipPath = fSourceZip.getParentFile().getAbsolutePath(); File temp = new File(zipPath); temp.mkdir(); temp.setExecutable(true); temp.setReadable(true); temp.setWritable(true); ZipFile zipFile = new ZipFile(fSourceZip); Enumeration e = zipFile.entries(); boolean createFolder = true; while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); File destinationFilePath = new File(zipPath, entry.getName()); destinationFilePath.setExecutable(true); destinationFilePath.setReadable(true); destinationFilePath.setWritable(true); if (createFolder) { destinationFilePath.getParentFile().mkdirs(); } if (entry.isDirectory()) { continue; } else { createFolder = true; BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(entry)); int b; byte buffer[] = new byte[1024]; FileOutputStream fos = new FileOutputStream( destinationFilePath); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); while ((b = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, b); } bos.flush(); bos.close(); bis.close(); } fileSize = fileSize + destinationFilePath.length(); } zipFile.close(); fSourceZip.delete(); }
推荐阅读
-
java file zip and upzip 博客分类: zip zipjava
-
Java 解压缩助手 博客分类: Java zip解压压缩java解压缩ZipArchiveEntry
-
java以zip格式实现压缩解压,有界面 博客分类: javazip界面压缩解压 javazip界面压缩解压
-
java压缩zip文件乱码问题 博客分类: Java JavaZip压缩文件中文乱码乱码
-
java zip解压缩 博客分类: Java zip解压缩compressZipArchiveEntryjava解压缩zip
-
java实现zip与unzip 博客分类: JAVA JAVAzip
-
利用java解压.zip的压缩文件 博客分类: J2EE java解压zip
-
JAVA ZIP压缩打包下载 博客分类: java 技术 zipjava
-
Java实现Zip压缩,解压缩(二) 博客分类: Java codepaper javazipunzip
-
zip版tomcat(6,7)不能注册成windows服务或者注册后不能启动的问题解决 博客分类: Java其他分类微服务数据库