JAVA解压.Z及.ZIP文件
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.20</version> </dependency>
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文件