当前位置: 首页 > 工具软件 > TrueZIP > 使用案例 >

Truezip使用实例

公西苗宣
2023-12-01

Truezip东西做的好,可惜帮助写的不咋地,压缩和解压缩的示例都没提供好。

 

压缩文件的例子

File src = new File("abc.txt");
File dst = new File("abc.zip/abc.txt");
boolean success = src.copyTo(dst);
解压文件的例子

        File src = new File("xyz.zip/xyz.txt");
        File dst = new File("xyz.txt");
        boolean success = src.copyTo(dst);
解决中文文件名问题的例子

import de.schlichtherle.io.ArchiveDetector;
import de.schlichtherle.io.DefaultArchiveDetector;
import de.schlichtherle.io.archive.tar.TarBZip2Driver;
import de.schlichtherle.io.archive.tar.TarDriver;
import de.schlichtherle.io.archive.tar.TarGZipDriver;
import de.schlichtherle.io.archive.zip.CheckedZip32Driver;


public class ZipTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int a = zipSingleFile("D:\\ziptest\\中文haha.txt", "D:\\ziptest\\中文呵呵.zip");
        int b = unzipSingleFile("D:\\ziptest\\中文呵呵.zip", "D:\\ziptest\\中文嘿嘿.txt");
    }

    
    /**
     * 压缩一个文件
     * 
     * @param srcFileName
     * @param zipFileName
     * @return
     */
    public static int zipSingleFile(String srcFileName, String zipFileName) {
        return zipSingleFile(srcFileName, zipFileName, "GBK");
    }

    public static int zipSingleFile(String srcFileName, String zipFileName,
            String encoding) {
        ArchiveDetector detector = new DefaultArchiveDetector(
                ArchiveDetector.ALL, new Object[] { "zip",
                        new CheckedZip32Driver(encoding), "tar",
                        new TarDriver(encoding), "tgz|tar.gz",
                        new TarGZipDriver(encoding), "tbz|tar.bz2",
                        new TarBZip2Driver(encoding) });

        String srcShortFilename = srcFileName.substring(srcFileName
                .lastIndexOf('\\'));

        boolean result = new de.schlichtherle.io.File(srcFileName, detector)
                .copyTo(new de.schlichtherle.io.File(zipFileName + "/"
                        + srcShortFilename, detector));
        if (result)
            return 0;
        else
            return -1;
    }

    /**
     * 解压一个文件
     * 
     * @param srcFileName
     * @param destFileName
     * @return
     */
    public static int unzipSingleFile(String srcFileName, String destFileName) {
        return unzipSingleFile(srcFileName, destFileName, "GBK");
    }

    public static int unzipSingleFile(String srcZipFileName,
            String destFileName, String encoding) {

        ArchiveDetector detector = new DefaultArchiveDetector(
                ArchiveDetector.ALL, new Object[] { "zip",
                        new CheckedZip32Driver(encoding), "tar",
                        new TarDriver(encoding), "tgz|tar.gz",
                        new TarGZipDriver(encoding), "tbz|tar.bz2",
                        new TarBZip2Driver(encoding) });

        de.schlichtherle.io.File zipfile = new de.schlichtherle.io.File(
                srcZipFileName, detector);
        if (!zipfile.isArchive())
            return -1;
        String files[] = zipfile.list();
        if (files == null || files.length == 0) {
            return -2;
        }
        boolean result = new de.schlichtherle.io.File(zipfile, files[0],
                detector).copyTo(new de.schlichtherle.io.File(destFileName,
                detector));
        if (result)
            return 0;
        else
            return -3;
    }
}

压缩目录的时候使用copyAllTo等方法即可。

 类似资料: