Java Code Examples for net.lingala.zip4j.core.ZipFile#setRunInThread()

The following examples show how to use net.lingala.zip4j.core.ZipFile#setRunInThread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * 压缩多个文件
 *
 * @param list                被压缩的文件集合
 * @param destinationFilePath 压缩后生成的文件路径
 * @param password            压缩 密码
 * @param callback            回调
 */
public static void zip(ArrayList<File> list, String destinationFilePath, String password, final IZipCallback callback) {
    if (list == null || list.size() == 0 || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFilePath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("zip: list=" + list.toString() + " , destinationFilePath=" + destinationFilePath + " , password=" + password);
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(8);
        parameters.setCompressionLevel(5);
        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(99);
            parameters.setAesKeyStrength(3);
            parameters.setPassword(password);
        }
        ZipFile zipFile = new ZipFile(destinationFilePath);
        zipFile.setRunInThread(true);
        zipFile.addFiles(list, parameters);
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("zip: Exception=" + e.getMessage());
    }
}
 
Example 2
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * 解压
 *
 * @param targetZipFilePath     待解压目标文件地址
 * @param destinationFolderPath 解压后文件夹地址
 * @param password              解压密码
 * @param callback              回调
 */
public static void unzip(String targetZipFilePath, String destinationFolderPath, String password, final IZipCallback callback) {
    if (!Zip4jUtil.isStringNotNullAndNotEmpty(targetZipFilePath) || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFolderPath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("unzip: targetZipFilePath=" + targetZipFilePath + " , destinationFolderPath=" + destinationFolderPath + " , password=" + password);
    try {
        ZipFile zipFile = new ZipFile(targetZipFilePath);
        if (zipFile.isEncrypted() && Zip4jUtil.isStringNotNullAndNotEmpty(password)) {
            zipFile.setPassword(password);
        }
        zipFile.setRunInThread(true);
        zipFile.extractAll(destinationFolderPath);
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("unzip: Exception=" + e.getMessage());
    }
}
 
Example 3
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 6 votes vote down vote up
/**
 * 解压文件到指定目录
 *
 * @param zipFile      压缩文件
 * @param outDir       输出路径
 * @param isClean      是否清理目录
 * @return
 */
public static boolean unzip(File zipFile, File outDir, boolean isClean){
    try {
        if(!FileHelper.exists(zipFile)){
            return false;
        }
        ZipFile zip = new ZipFile(zipFile);
        if(isClean && outDir.exists()){
            FileHelper.delete(outDir);
        }
        zip.setRunInThread(false);
        zip.extractAll(outDir.getPath());
        return true;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 4
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 6 votes vote down vote up
/**
 * 解压文件,注意该法方法并不是异步的
 *
 * @param zipFile      压缩文件
 * @param filter
 * @return
 */
public static void list(File zipFile, FileFilter filter){
    try {
        if(!FileHelper.exists(zipFile)){
            return;
        }
        ZipFile zip = new ZipFile(zipFile);
        zip.setRunInThread(false);

        // Get the list of file headers from the zip file
        List fileHeaderList = zip.getFileHeaders();

        // Loop through the file headers
        for (int i = 0; i < fileHeaderList.size(); i++) {
            // Extract the file to the specified destination
            filter.handle(zip, (FileHeader) fileHeaderList.get(i));
        }
    } catch (ZipException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩文件或者文件夹
 *
 * @param targetPath          被压缩的文件路径
 * @param destinationFilePath 压缩后生成的文件路径
 * @param password            压缩加密 密码
 * @param callback            压缩进度回调
 */
public static void zip(String targetPath, String destinationFilePath, String password, IZipCallback callback) {
    if (!Zip4jUtil.isStringNotNullAndNotEmpty(targetPath) || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFilePath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("zip: targetPath=" + targetPath + " , destinationFilePath=" + destinationFilePath + " , password=" + password);
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(8);
        parameters.setCompressionLevel(5);
        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(99);
            parameters.setAesKeyStrength(3);
            parameters.setPassword(password);
        }
        ZipFile zipFile = new ZipFile(destinationFilePath);
        zipFile.setRunInThread(true);
        File targetFile = new File(targetPath);
        if (targetFile.isDirectory()) {
            zipFile.addFolder(targetFile, parameters);
        } else {
            zipFile.addFile(targetFile, parameters);
        }
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("zip: Exception=" + e.getMessage());
    }
}
 
Example 6
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
public static List<FileHeader> listForPattern(File zipFile, String regex){
        List<FileHeader> list = new ArrayList<>();
        Pattern p = Pattern.compile(regex);
//        Pattern p = Pattern.compile("[^/]*\\.dex");
        try {
            if(!FileHelper.exists(zipFile)){
                return new ArrayList<>(0);
            }
            ZipFile zip = new ZipFile( zipFile);
            zip.setRunInThread(false);

            // Get the list of file headers from the zip file
            List fileHeaderList = zip.getFileHeaders();

            // Loop through the file headers
            for (int i = 0; i < fileHeaderList.size(); i++) {
                // Extract the file to the specified destination
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                Matcher matcher = p.matcher(fileHeader.getFileName());
                if(matcher.matches()){
                    list.add(fileHeader);
                }
            }
            return list;
        } catch (ZipException e) {
            e.printStackTrace();
        }
        return new ArrayList<>(0);
    }