首页 > 代码库 > 【转】JAVA解压.TAR.Z及.ZIP文件
【转】JAVA解压.TAR.Z及.ZIP文件
解压.ZIP文件
package app.qdupr.Method; import java.io.File; 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; /** * ZIP文件解压 * @author Administrator * */ public class ZipUncompress { private static int bufSize = 8096; //size of byte public static void main(String[] args) throws Exception { String tmpFile = "C:\\Users\\Administrator\\Desktop\\20151101.rar"; ZipUncompress.unZip(tmpFile, tmpFile.substring(0, tmpFile.lastIndexOf("."))); // ----------------- System.out.println("Usage:"); System.out.println("解压:java ZipFileUtils -unzip fileName.zip"); System.out.println(); } public static String unZip(String unZipFileName, String destFileName) { File unzipFile = new File(unZipFileName); if(destFileName == null || destFileName.trim().length() == 0) { destFileName = unzipFile.getParent(); } File destFile; ZipFile zipFile = null; try { zipFile = new ZipFile(unzipFile, "GBK"); for(Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); destFile = new File(destFileName, entry.getName()); unZipFile(destFile, zipFile, entry); //执行解压 } } catch(Exception e) { return e.getMessage(); } finally { try { assert zipFile != null; zipFile.close(); } catch(Exception e) { e.printStackTrace(); } } return null; } private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry) throws IOException { InputStream inputStream; FileOutputStream fileOut; if(entry.isDirectory()) //是目录,则创建之 { destFile.mkdirs(); } else //是文件 { //如果指定文件的父目录不存在,则创建之. File parent = destFile.getParentFile(); if(parent != null && !parent.exists()) { parent.mkdirs(); } inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(destFile); byte[] buf = new byte[bufSize]; int readedBytes; while((readedBytes = inputStream.read(buf)) > 0) { fileOut.write(buf, 0, readedBytes); } fileOut.close(); inputStream.close(); } } }
解压.TAR.Z文件
所需jar包 commons-compress-1.7及以上
所需jar包 commons-compress-1.7及以上
package app.qdupr.Method; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.z.ZCompressorInputStream; /** * 自动解压.Z格式的文件 * @author Administrator * */ public class ZCompress { /** * 测试方法 * @param args */ public static void main(String[] args) { ZCompress zip = new ZCompress(); File dir = new File("E:\\Test"); File[] subs = dir.listFiles(); for (File sub : subs) { String file=sub.getParent()+File.separator+sub.getName(); zip.uncompress(file); System.out.println(file); } } /** * 获取文件夹下全部文件 * @param file */ public static void findAllfile(String file) { ZCompress zip = new ZCompress(); File dir = new File(file); File[] subs = dir.listFiles(); for (File sub : subs) { String newfile=sub.getParent()+File.separator+sub.getName(); zip.uncompress(newfile); } } /** * .Z文件解压 * @param file */ public void uncompress(String file) { //File file =new File("E:\\BEA20151101.Z"); ZCompress.deCompressTZFile(file); } /** * 将String型转换为File类型 * @param file * @return */ public static File deCompressTZFile(String file) { return deCompressZFile(new File(file)); } /** * 执行解压 * @param file * @return */ private static File deCompressZFile(File file) { int buffersize = 2048; FileOutputStream out = null; ZCompressorInputStream zIn = null; try { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); //解压后的文件存放路径及文件名 String name = file.getName().substring(0, file.getName().indexOf(".")); File outFile = new File("E:\\File\\"+name); out = new FileOutputStream(outFile); zIn = new ZCompressorInputStream(in); final byte[] buffer = new byte[buffersize]; int n = 0; while (-1 != (n = zIn.read(buffer))) { out.write(buffer, 0, n); } return outFile; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { out.close(); zIn.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * .TAR格式文件解压 * @param file */ private static void deCompressTARFile(File file) { int buffersize = 2048; String basePath = file.getParent() + File.separator; TarArchiveInputStream is = null; try { is = new TarArchiveInputStream(new FileInputStream(file)); while (true) { TarArchiveEntry entry = is.getNextTarEntry(); if (entry == null) { break; } if (entry.isDirectory()) {// 这里貌似不会运行到,跟ZipEntry有点不一样 new File(basePath + entry.getName()).mkdirs(); } else { FileOutputStream os = null; try { File f = new File(basePath + entry.getName()); if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } if (!f.exists()) { f.createNewFile(); } os = new FileOutputStream(f); byte[] bs = new byte[buffersize]; int len = -1; while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { os.close(); } } } } catch (Exception e) { e.printStackTrace(); } finally { try { is.close(); file.delete(); } catch (IOException e) { e.printStackTrace(); } } } }
【转】JAVA解压.TAR.Z及.ZIP文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。