一、前言
基于apache的ant.jar包中org.apache.tools.tar.*定义TarUtils解压缩工具类,对linux下的文件进行tar命令的常见解压缩文件操作,详情参见代码示例说明。
二、代码示例
import java.io.BufferedOutputStream;@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.util.ArrayList;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.zip.CRC32;@b@import java.util.zip.CheckedOutputStream;@b@import java.util.zip.GZIPInputStream;@b@import java.util.zip.GZIPOutputStream;@b@import java.util.zip.ZipEntry;@b@import java.util.zip.ZipFile;@b@import java.util.zip.ZipOutputStream;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.apache.tools.tar.TarEntry;@b@import org.apache.tools.tar.TarInputStream;@b@import org.apache.tools.tar.TarOutputStream;@b@@b@public class TarUtils {@b@ @b@ private static Log logger = LogFactory.getLog(TarUtils.class);@b@ private static final int BUFFER = 4096;@b@@b@ public static void tarFile(String inputFileName, String targetFileName,@b@ boolean includeRootDir) {@b@ File inputFile = new File(inputFileName);@b@ if ((inputFile.isDirectory()) && (!(includeRootDir))) {@b@ List fileList = new ArrayList();@b@ File[] files = inputFile.listFiles();@b@ for (int i = 0; i < files.length; ++i)@b@ fileList.add(files[i].getAbsolutePath());@b@@b@ tarFiles(fileList, targetFileName);@b@ } else {@b@ tarFile(inputFileName, targetFileName);@b@ }@b@ }@b@@b@ private static void tarFile(String inputFileName, String targetFileName) {@b@ File inputFile = new File(inputFileName);@b@ String base = inputFileName.substring(inputFileName@b@ .lastIndexOf(File.separator) + 1);@b@ TarOutputStream out = getTarOutputStream(targetFileName);@b@ tarPack(out, inputFile, base);@b@ try {@b@ if (null != out)@b@ out.close();@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@ }@b@@b@ public static void tarFiles(List inputFileNameList, String targetFileName) {@b@ TarOutputStream out = getTarOutputStream(targetFileName);@b@@b@ for (Iterator iterator = inputFileNameList.iterator(); iterator@b@ .hasNext();) {@b@ String inputFileName = (String) iterator.next();@b@ File inputFile = new File(inputFileName);@b@ String base = inputFileName.substring(inputFileName@b@ .lastIndexOf(File.separator) + 1);@b@ tarPack(out, inputFile, base);@b@ }@b@ try {@b@ if (null != out)@b@ out.close();@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@ }@b@@b@ private static void tarPack(TarOutputStream out, File inputFile, String base) {@b@ if (inputFile.isDirectory()) {@b@ packFolder(out, inputFile, base);@b@ } else@b@ packFile(out, inputFile, base);@b@ }@b@@b@ private static void packFolder(TarOutputStream out, File inputFile,@b@ String base) {@b@ File[] fileList = inputFile.listFiles();@b@ try {@b@ out.putNextEntry(new TarEntry(base + "/"));@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@ base = base + "/";@b@ int i = 0;@b@ for (int j = fileList.length; i < j; ++i) {@b@ File file = fileList[i];@b@ tarPack(out, file, base + file.getName());@b@ }@b@ }@b@@b@ private static void packFile(TarOutputStream out, File inputFile,@b@ String base) {@b@ TarEntry tarEntry = new TarEntry(base);@b@@b@ tarEntry.setSize(inputFile.length());@b@ try {@b@ out.putNextEntry(tarEntry);@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@ FileInputStream in = null;@b@ try {@b@ in = new FileInputStream(inputFile);@b@ } catch (FileNotFoundException e) {@b@ logger.error(e);@b@ }@b@ int b = 0;@b@ byte[] B_ARRAY = new byte[4096];@b@ try {@b@ while ((b = in.read(B_ARRAY, 0, 4096)) != -1)@b@ out.write(B_ARRAY, 0, b);@b@ } catch (IOException e) {@b@ logger.error(e);@b@ } catch (NullPointerException e) {@b@ logger.error(e);@b@ } finally {@b@ try {@b@ if (null != in)@b@ in.close();@b@@b@ if (null != out)@b@ out.closeEntry();@b@ } catch (IOException e) {@b@ }@b@ }@b@ }@b@@b@ private static void compress(File srcFile) {@b@ File target = new File(srcFile.getAbsolutePath() + ".gz");@b@ FileInputStream in = null;@b@ GZIPOutputStream out = null;@b@ try {@b@ in = new FileInputStream(srcFile);@b@ out = new GZIPOutputStream(new FileOutputStream(target));@b@ int number = 0;@b@ byte[] B_ARRAY = new byte[4096];@b@ while ((number = in.read(B_ARRAY, 0, 4096)) != -1)@b@ out.write(B_ARRAY, 0, number);@b@ } catch (FileNotFoundException e) {@b@ logger.error(e);@b@ } catch (IOException e) {@b@ logger.error(e);@b@ } finally {@b@ try {@b@ if (in != null) {@b@ in.close();@b@ }@b@@b@ if (out != null)@b@ out.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ }@b@@b@ private static TarOutputStream getTarOutputStream(String targetFileName) {@b@ targetFileName = targetFileName + ".tar";@b@ FileOutputStream fileOutputStream = null;@b@ try {@b@ fileOutputStream = new FileOutputStream(targetFileName);@b@ } catch (FileNotFoundException e) {@b@ logger.error(e);@b@ }@b@ BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(@b@ fileOutputStream);@b@ TarOutputStream out = new TarOutputStream(bufferedOutputStream);@b@@b@ out.setLongFileMode(2);@b@ return out;@b@ }@b@@b@ public static void unTgzFile(File file, String descDir) {@b@ GZIPInputStream gzi = null;@b@ try {@b@ gzi = new GZIPInputStream(new FileInputStream(file));@b@ } catch (FileNotFoundException e) {@b@ logger.error(e);@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@@b@ TarInputStream tin = new TarInputStream(gzi);@b@ try {@b@ TarEntry tarEntry=null;@b@ while ((tarEntry = tin.getNextEntry()) != null) {@b@ unTarPack(tin, tarEntry, descDir);@b@ }@b@ } catch (IOException e) {@b@ logger.error(e);@b@ } finally {@b@ try {@b@ tin.close();@b@ } catch (Exception e) {@b@ logger.error(e);@b@ }@b@ }@b@ }@b@@b@ public static void unTarFile(File file, String descDir) {@b@ TarInputStream tin = null;@b@ try {@b@ tin = new TarInputStream(new FileInputStream(file));@b@ TarEntry tarEntry;@b@ while ((tarEntry = tin.getNextEntry()) != null) {@b@ unTarPack(tin, tarEntry, descDir);@b@ }@b@ } catch (IOException e) {@b@ logger.error(e);@b@ } finally {@b@ try {@b@ tin.close();@b@ } catch (Exception e) {@b@ logger.error(e);@b@ }@b@ }@b@ }@b@@b@ private static void unTarPack(TarInputStream tin, TarEntry tarEntry,@b@ String descDir) {@b@ if (tarEntry.isDirectory())@b@ unPackFolder(tin, tarEntry, descDir);@b@ else@b@ unPackFile(tin, tarEntry, descDir);@b@ }@b@@b@ private static void unPackFolder(TarInputStream tin, TarEntry tarEntry,@b@ String descDir) {@b@ descDir = descDir + File.separator + tarEntry.getName();@b@ if (!(new File(descDir).exists()))@b@ new File(descDir).mkdirs();@b@ }@b@@b@ private static void unPackFile(TarInputStream tin, TarEntry tarEntry,@b@ String descDir) {@b@ if (!(new File(descDir).exists()))@b@ new File(descDir).mkdirs();@b@ try {@b@ tin.copyEntryContents(new FileOutputStream(descDir + File.separator@b@ + tarEntry.getName()));@b@ } catch (FileNotFoundException e) {@b@ logger.error(e);@b@ } catch (IOException e) {@b@ logger.error(e);@b@ }@b@ }@b@@b@ public static void zipFile(File srcfile, File zipfile) {@b@ byte[] buf = new byte[1024];@b@ try {@b@ CheckedOutputStream cos = new CheckedOutputStream(@b@ new FileOutputStream(zipfile), new CRC32());@b@ ZipOutputStream out = new ZipOutputStream(cos);@b@@b@ FileInputStream in = new FileInputStream(srcfile);@b@@b@ out.putNextEntry(new ZipEntry(srcfile.getName()));@b@ int len;@b@ while ((len = in.read(buf)) > 0) {@b@ out.write(buf, 0, len);@b@ }@b@@b@ out.closeEntry();@b@ in.close();@b@@b@ out.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static void zipFile(String inputFileName, String targetFileName,@b@ boolean includeRootDir) {@b@ File inputFile = new File(inputFileName);@b@ File targetFile = new File(targetFileName);@b@ if ((inputFile.isDirectory()) && (!(includeRootDir))) {@b@ List fileList = new ArrayList();@b@ File[] files = inputFile.listFiles();@b@ for (int i = 0; i < files.length; ++i)@b@ fileList.add(files[i].getAbsolutePath());@b@@b@ zipFiles(fileList, targetFile);@b@ } else {@b@ zipFile(inputFile, targetFile);@b@ }@b@ }@b@@b@ public static void zipFiles(List srcfiles, File zipfile) {@b@ byte[] buf = new byte[1024];@b@ try {@b@ CheckedOutputStream cos = new CheckedOutputStream(@b@ new FileOutputStream(zipfile), new CRC32());@b@ ZipOutputStream out = new ZipOutputStream(cos);@b@@b@ for (Iterator iterator = srcfiles.iterator(); iterator.hasNext();) {@b@ String srcfilePath = (String) iterator.next();@b@ File srcfile = new File(srcfilePath);@b@ FileInputStream in = new FileInputStream(srcfile);@b@@b@ out.putNextEntry(new ZipEntry(srcfile.getName()));@b@ int len;@b@ while ((len = in.read(buf)) > 0) {@b@ out.write(buf, 0, len);@b@ }@b@@b@ out.closeEntry();@b@ in.close();@b@ }@b@@b@ out.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static void unZipFile(File zipfile, String descDir) {@b@ ZipFile zf;@b@ try {@b@ zf = new ZipFile(zipfile);@b@ for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {@b@ ZipEntry entry = (ZipEntry) entries.nextElement();@b@ String zipEntryName = entry.getName();@b@ InputStream in = zf.getInputStream(entry);@b@ OutputStream out = new FileOutputStream(descDir + zipEntryName);@b@ byte[] buf1 = new byte[1024];@b@ @b@ int len;@b@ while ((len = in.read(buf1)) > 0) {@b@ out.write(buf1, 0, len);@b@ }@b@@b@ in.close();@b@ out.close();@b@ }@b@ zf.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static byte[] zipBytes(byte[] contentBytes) {@b@ ByteArrayOutputStream output = new ByteArrayOutputStream();@b@ byte[] buf = new byte[1024];@b@ try {@b@ CheckedOutputStream cos = new CheckedOutputStream(output,@b@ new CRC32());@b@ ZipOutputStream out = new ZipOutputStream(cos);@b@@b@ ByteArrayInputStream in = new ByteArrayInputStream(contentBytes);@b@@b@ out.putNextEntry(new ZipEntry("proPboc"));@b@ int len;@b@ while ((len = in.read(buf)) > 0) {@b@ @b@ out.write(buf, 0, len);@b@ }@b@@b@ out.closeEntry();@b@ in.close();@b@@b@ out.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ return output.toByteArray();@b@ }@b@@b@}