Java Code Examples for net.lingala.zip4j.exception.ZipException#printStackTrace()

The following examples show how to use net.lingala.zip4j.exception.ZipException#printStackTrace() . 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: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例2 创建压缩包添加文件到指定目录中进行压缩
 */
@Test
public void example2(){
	try {
           ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.zip");
           ArrayList<File> filesToAdd = new ArrayList<File>();
           filesToAdd.add(new File("src/main/resources/sample.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
            
           ZipParameters parameters = new ZipParameters();
           parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            
           parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
           parameters.setRootFolderInZip("target/");

           zipFile.addFiles(filesToAdd, parameters);
       } catch (ZipException e) {
           e.printStackTrace();
       } 
}
 
Example 2
Source File: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 添加多个文件到zip中指定的文件夹中
 * @param inPath
 * @param storagePath
 * @param outPath
 * @param password
 * @return
 */
public static boolean addFilesInZip(ArrayList<File> inFiles,String storagePath,String outPath,String password) {
	try {
		ArrayList filesToAdd = new ArrayList();  
		ZipFile zipFile = new ZipFile(outPath);
		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
		parameters.setRootFolderInZip(storagePath);  
		if(password!=null&&!password.equals("")){
			parameters.setEncryptFiles(true);  
			parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
			parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
			parameters.setPassword(password); 
		}
		zipFile.addFiles(inFiles, parameters);
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 3
Source File: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 从zip中删除文件
 * @param inPath
 * @param storagePath
 * @param password
 * @return
 */
public static boolean removeFileInZip(String inPath,String storagePath,String password) {
	try {
		ZipFile zipFile = new ZipFile(inPath);
		if (zipFile.isEncrypted()) {  
			zipFile.setPassword(password);  
		}  
		List fileHeaderList = zipFile.getFileHeaders();  
		storagePath = storagePath.replaceAll("\\\\", "/");
		for (int i =fileHeaderList.size() -1; i>0 ; i--) {  
		    FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); 
		    if(fileHeader.getFileName().indexOf(storagePath)==0){
		    	System.out.println("Name: " + fileHeader.getFileName());  
		    	zipFile.removeFile(fileHeader.getFileName());	
		    }
		}  
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 4
Source File: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 查看压缩包的文件列表
 * @param inPath
 * @param password
 * @return
 */
public static boolean getNameFromZip(String inPath,String password) {
	try {
		ZipFile zipFile = new ZipFile(inPath);
		if (zipFile.isEncrypted()) {  
			zipFile.setPassword(password);  
		}  
		List fileHeaderList = zipFile.getFileHeaders();  
		  
		for (int i = 0; i < fileHeaderList.size(); i++) {  
		    FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); 
		    System.out.println("Name: " + fileHeader.getFileName());  
		    System.out.println("Compressed Size: " + fileHeader.getCompressedSize());  
		    System.out.println("Uncompressed Size: " + fileHeader.getUncompressedSize());  
		    System.out.println("CRC: " + fileHeader.getCrc32());  
		    System.out.println("************************************************************");  
		}  
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 5
Source File: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例3 添加文件夹到压缩包中
 */
@Test
public void example3(){
	try {
           
           ZipFile zipFile = new ZipFile("src/main/resources/AddFolder.zip");
           String folderToAdd = "src/main/resources";
        
           ZipParameters parameters = new ZipParameters();
            
           parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
           parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
           zipFile.addFolder(folderToAdd, parameters);
            
       } catch (ZipException e) {
           e.printStackTrace();
       }
}
 
Example 6
Source File: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例7 解压压缩文件
 */
@Test
public void example7(){
	
	example4();
	
	try {
           ZipFile zipFile = new ZipFile("src/main/resources/AddFilesWithAESZipEncryption.zip");
           if (zipFile.isEncrypted()) {
               // if yes, then set the password for the zip file
               zipFile.setPassword("123456");
               zipFile.extractAll("src/main/resources/zipfile");
           }
       } catch (ZipException e) {
           e.printStackTrace();
       }
}
 
Example 7
Source File: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例1 创建压缩包添加文件到压缩包中(未设置加密)
 */
@Test
public void example1(){
	try {
           
           ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.zip");
            
           ArrayList<File> filesToAdd = new ArrayList<File>();
           filesToAdd.add(new File("src/main/resources/sample.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
            
           ZipParameters parameters = new ZipParameters();
           parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
           parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
           zipFile.addFiles(filesToAdd, parameters);
           
           CompressUtil.printZipInfo("src/main/resources/AddFilesDeflateComp.zip");
           
           
       } catch (ZipException e) {
           e.printStackTrace();
       }
}
 
Example 8
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 9
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 10
Source File: BackupManager.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isBackupPasswordProtected(File file) {
    try {
        ZipFile zipFile = new ZipFile(file);
        FileHeader preferences = zipFile.getFileHeader(BACKUP_PREFERENCES_PATH);
        return preferences.isEncrypted();
    } catch (ZipException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 11
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 解压指定文件
 *
 * @param zip
 * @param fileHeader
 * @param destPath
 * @return
 */
public static boolean unzip(ZipFile zip, FileHeader fileHeader, File destPath){
    try {
        zip.extractFile(fileHeader, destPath.getAbsolutePath());
        return true;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 12
Source File: JiaGu.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 加密dex
 *
 * @param decompileDir
 * @return
 */
private static boolean encryptDex(File apk, File decompileDir) {

    File dexFile = new File(decompileDir, "classes.dex");
    if (dexFile.exists()) {
        dexFile.delete();
    }

    try {
        // 解压apk中的classes.dex
        ZipFile zipFile = new ZipFile(apk);
        ZipUtils.unzip(zipFile, "classes.dex", dexFile.getParentFile());
    } catch (ZipException e) {
        e.printStackTrace();
        return false;
    }

    File assets = new File(decompileDir, "assets");
    assets.mkdirs();

    File encryptFile = new File(assets, JIAGU_DATA_BIN);
    encryptFile.delete();

    EncryptUtils.encrypt(dexFile, encryptFile);
    dexFile.delete();

    return true;
}
 
Example 13
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 是否包含指定文件
 *
 * @param zip
 * @param fileName
 * @return
 */
public static boolean hasFile(ZipFile zip, String fileName){
    try {
        return zip.getFileHeader(fileName) != null;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 14
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 是否包含指定文件
 *
 * @param zipFile
 * @param fileName
 * @return
 */
public static boolean hasFile(File zipFile, String fileName){
    try {
        if(!FileHelper.exists(zipFile)){
            return false;
        }
        return hasFile(new ZipFile(zipFile),fileName);
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 15
Source File: GtfsFeedImpl.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
protected String unzip(String compressedZip) {
	String unzippedFolder = compressedZip.substring(0, compressedZip.length() - 4) + "/";
	log.info("Unzipping " + compressedZip + " to " + unzippedFolder);

	try {
		ZipFile zipFile = new ZipFile(compressedZip);
		if(zipFile.isEncrypted()) {
			throw new RuntimeException("Zip file is encrypted");
		}
		zipFile.extractAll(unzippedFolder);
	} catch (ZipException e) {
		e.printStackTrace();
	}
	return unzippedFolder;
}
 
Example 16
Source File: PFileIO.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Unzip a file into a folder", example = "")
@PhonkMethodParam(params = {"zipFile", "folder"})
public void unzip(final String src, final String dst, final ReturnInterface callback) {
    Thread t = new Thread(() -> {
        try {
            FileIO.unZipFile(getAppRunner().getProject().getFullPathForFile(src), getAppRunner().getProject().getFullPathForFile(dst));
            returnValues(null, callback);
        } catch (ZipException e) {
            e.printStackTrace();
        }
    });
    t.start();
}
 
Example 17
Source File: PhonkScriptHelper.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public static boolean installProject(Context c, String from, String to) {
    try {
        FileIO.unZipFile(from, to);
        return true;
    } catch (ZipException e) {
        e.printStackTrace();
    }

    return false;
}
 
Example 18
Source File: RxZipTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
public static String zipEncrypt(String src, String dest, boolean isCreateDir, String passwd) {
    File srcFile = new File(src);
    dest = buildDestinationZipFilePath(srcFile, dest);
    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);           // 压缩方式
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);    // 压缩级别
    if (!RxDataTool.isNullString(passwd)) {
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
        parameters.setPassword(passwd.toCharArray());
    }
    try {
        net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(dest);
        if (srcFile.isDirectory()) {
            // 如果不创建目录的话,将直接把给定目录下的文件压缩到压缩文件,即没有目录结构
            if (!isCreateDir) {
                File [] subFiles = srcFile.listFiles();
                ArrayList<File> temp = new ArrayList<File>();
                Collections.addAll(temp, subFiles);
                zipFile.addFiles(temp, parameters);
                return dest;
            }
            zipFile.addFolder(srcFile, parameters);
        } else {
            zipFile.addFile(srcFile, parameters);
        }
        return dest;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 19
Source File: Zip4j.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 示例4 创建加密压缩包
 */
@Test
public void example4(){
	try {
           ZipFile zipFile = new ZipFile("src/main/resources/AddFilesWithAESZipEncryption.zip");

           ArrayList<File> filesToAdd = new ArrayList<File>();
           filesToAdd.add(new File("src/main/resources/sample.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
           
           ZipParameters parameters = new ZipParameters();
           parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            
           parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
           parameters.setEncryptFiles(true);
            
           parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
            

           parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
           parameters.setPassword("123456");
    
           zipFile.addFiles(filesToAdd, parameters);
       } catch (ZipException e) {
           e.printStackTrace();
       }
}
 
Example 20
Source File: Utility.java    From APKRepatcher with MIT License 5 votes vote down vote up
/**
 * Jar sign creates CERT SF & RSA but some apk have different name like
 * ABC.SF This module will rename the CERT.SF & CERT.RSA to the new
 * certificate name
 * 
 * @param newAPKPath
 *            Path of the modified apk
 */
public static void repackCert(String newAPKPath) {

	try {
		ZipFile zipFile = new ZipFile(newAPKPath);
		zipFile.extractFile("META-INF" + File.separator + "CERT.RSA",
				getProjectPath());
		zipFile.extractFile("META-INF" + File.separator + "CERT.SF",
				getProjectPath());
		zipFile.removeFile("META-INF" + File.separator + "CERT.SF");
		zipFile.removeFile("META-INF" + File.separator + "CERT.RSA");
		File rsaFile = new File(getProjectPath() + File.separator
				+ "META-INF" + File.separator + "CERT.RSA");
		File sfFile = new File(getProjectPath() + File.separator
				+ "META-INF" + File.separator + "CERT.SF");
		File newCertFolder = new File(getProjectPath() + File.separator
				+ "META-INF");
		rsaFile.renameTo(new File(getProjectPath() + File.separator
				+ "META-INF" + File.separator + certificateName + ".RSA"));
		sfFile.renameTo(new File(getProjectPath() + File.separator
				+ "META-INF" + File.separator + certificateName + ".SF"));
		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
		zipFile.addFolder(newCertFolder, parameters);

	} catch (ZipException e) {
		e.printStackTrace();
	}
}