问题描述:有主目录(maindir),下面有多个子目录,子目录下若干个文件
待解决问题:如何将目录压缩成.gz格式的压缩文件,并解压,并按行读取目录下的文件的内容

解决方案 »

  1.   

    楼主这个用tar命令就可以了阿
    难道不会想要用java实现吧
      

  2.   

    A:LINUX下:01-.tar格式
    解包:[*******]$ tar xvf FileName.tar
    打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)02-.gz格式
    解压1:[*******]$ gunzip FileName.gz
    解压2:[*******]$ gzip -d FileName.gz
    压 缩:[*******]$ gzip FileName
    B:WINDOWS下:
    Total Commander用这个软件
      

  3.   


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;/**
     * 压缩实现类 <br>
     * 主要实现:  <br>
     *     <p>压缩单个文件、
     *     <p>压缩文件夹下的所有文件及子文件夹
     *  
     */public class ZipTool {    /**
         * 压缩单个文件
         * @param filePath    文件路径
         * @param fileName    文件名字
         * @param objDir      压缩文件目标文件夹
         * @param ojbZipName  压缩文件名字
         * @return
         * @throws IOException
         */
        public boolean zip(String filePath, String fileName, String objDir,
                String ojbZipName) throws IOException {
            boolean tag = false;
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objDir
                    + ojbZipName));        FileInputStream fis = new FileInputStream(filePath + fileName);
            byte[] b = new byte[4096];
            int i = 0;        zos.putNextEntry(new ZipEntry(fileName));
            while ((i = (fis.read(b))) > 0) {
                zos.write(b, 0, i);
            }        fis.close();
            zos.close();        return tag;
        }
        
        /**
         * 压缩一个文件夹下的所有文件  注意中间路径连接用"/"  如:c:/tt/ttt
         * @param srcPath     要压缩的文件夹路径
         * @param objZipPath  目标文件夹路径
         * @param ojbZipName  目标压缩文件名字
         * @return
         */
        public boolean zipDir(String srcPath, String objZipPath, String ojbZipName){        ZipOutputStream zos = null;
            try {
                File objFile = new File(objZipPath , ojbZipName);
                zos = new ZipOutputStream(new FileOutputStream(objFile));            if (srcPath == null) {
                    System.out.println("传入的源文件夹路径字符串不能为空!");
                    throw new java.lang.NullPointerException();
                }
                String dirName = "";
                File file = new File(srcPath);
                if (!file.isDirectory()) {
                    throw new Exception("传入了不正确的源文件夹路径!");
                } else {
                    dirName = srcPath.substring(srcPath.lastIndexOf("/") + 1);
                    if (dirName == null || "".equals(dirName)) {
                        String subStr = srcPath.substring(0, srcPath.length() - 2);
                        dirName = subStr.substring(subStr.lastIndexOf("/") + 1);
                    }
                    ZipEntry ze = new ZipEntry(dirName + "/");
                    zos.putNextEntry(ze);
                    if (dirName == null || "".equals(dirName)) {
                        throw new Exception("传入了不正确的源文件夹路径!");
                    }
                }            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {
                    zipFile(dirName + "/", files[i], zos);
                }
                return true;
            } catch (Exception e) {
                System.out.println("压缩文件时出现异常!");
                e.printStackTrace();
                return false;
            }finally{
                if(zos != null){
                    try {
                        zos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }            }    /**
         * 用来压缩文件夹下的所有子文件
         * 此方法为一个递归调法方法
         * @param zipPath 要压缩的文件在压缩包中的相对路径
         * @param file     要压缩的文件引用
         * @param zos   压缩文件输出流
         * @throws IOException
         */
        private void zipFile(String zipPath, File file, ZipOutputStream zos)
                throws IOException {
            //是文件夹的操作
            if (file.isDirectory()) {
                ZipEntry ze = new ZipEntry(zipPath + file.getName() + "/");
                zos.putNextEntry(ze);
                //递归调用
                for (int i = 0; i < file.listFiles().length; i++) {
                    zipFile(zipPath + file.getName() + "/", file.listFiles()[i], zos);
                }
            //是文件时的操作
            }else{
                FileInputStream fis = null;            
                try{
                    fis = new FileInputStream(file);
                    ZipEntry ze = new ZipEntry(zipPath + file.getName());
                    zos.putNextEntry(ze);
                    byte []b = new byte[4096]; 
                    int i = 0;
                    while(  ( i = (fis.read(b)) )  >  0  ){
                        zos.write(b, 0, i);
                    }
                }finally{
                    if(fis != null){
                        fis.close();
                    }
                }
            }
        }    public static void main(String[] args) {
            ZipTool zt = new ZipTool();
            try {
                zt.zipDir("c:/gif", "c:/", "test.zip");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}
      

  4.   

    windows下,还真没碰到过.gz的文件。
      

  5.   

    我想用java进行压缩、解压,不是用Linux命令
      

  6.   

    Runtime.exce
    调用脚本进行压缩和解压吧!
      

  7.   

    问个白痴问题?.gz是用什么压缩流压缩!gzip吗??
      

  8.   

    在处理压缩文件时,java使用ZipInputStream读入ZIP文件。要写出ZIP文件,可以使用ZipOutputStream。
      

  9.   

    4楼的方法在linux下能行的通吗?