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

The following examples show how to use net.lingala.zip4j.core.ZipFile#extractFile() . 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: 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 extractFileFromZip(String inPath,String storagePath,String outPath ,String password) {
	try {
		ZipFile zipFile = new ZipFile(inPath);
		if (zipFile.isEncrypted()) {  
			zipFile.setPassword(password);  
		}  
		List fileHeaderList = zipFile.getFileHeaders();  
		storagePath = storagePath.replaceAll("\\\\", "/");
		for (int i =0;i<fileHeaderList.size() ;i++) {  
		    FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); 
		    if(fileHeader.getFileName().indexOf(storagePath)==0){
		    	zipFile.extractFile(fileHeader, outPath);
		    	zipFile.removeFile(fileHeader.getFileName());	
		    }
		}  
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 2
Source File: RestoreAppsTasker.java    From KAM with GNU General Public License v3.0 6 votes vote down vote up
private void withData() throws Exception {
    FileUtil fileUtil = new FileUtil();
    File dataFile = new File(fileUtil.getBaseFolderName() + "data.zip");
    dataZip = new ZipFile(dataFile);
    if (dataZip.getFileHeaders() != null) {
        List fileHeaderList = dataZip.getFileHeaders();
        ProgressModel progressModel = new ProgressModel();
        progressModel.setMax(fileHeaderList.size());
        publishProgress(progressModel);
        List<FileHeader> headers = dataZip.getFileHeaders();
        for (FileHeader header : headers) {
            dataZip.extractFile(header, fileUtil.getBaseFolderName());
            copyFileToData(fileUtil.getBaseFolderName(), header.getFileName());
        }
    }
    FileUtils.forceDelete(dataFile);
}
 
Example 3
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();
	}
}
 
Example 4
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 5
Source File: ZipUtils.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 解压指定文件
 *
 * @param zip
 * @param fileName
 * @param destPath
 * @return
 */
public static boolean unzip(ZipFile zip, String fileName, File destPath){
    try {
        zip.extractFile(fileName, destPath.getAbsolutePath());
        return true;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 6
Source File: RestoreAppsTasker.java    From KAM with GNU General Public License v3.0 4 votes vote down vote up
@Override
    protected ProgressModel doInBackground(Context... params) {
        try {
            FileUtil fileUtil = new FileUtil();
            boolean withData = AppHelper.isRestoreData(params[0]);
            if (withData) RootManager.getInstance().obtainPermission();
            File zipFile = new File(fileUtil.getBaseFolderName() + "backup.zip");
            if (!zipFile.exists()) {
//                if (withData) {
//                    withData();
//                }
                return error("Backup Folder Doe Not Exits!");
            }
            zFile = new ZipFile(zipFile);
            List fileHeaderList = zFile.getFileHeaders();
            ProgressModel progressModel = new ProgressModel();
            progressModel.setMax(fileHeaderList.size());
            publishProgress(progressModel);
            for (int i = 0; i < fileHeaderList.size(); i++) {
                if (isCancelled()) {
                    return error("cancelled");
                }
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                zFile.extractFile(fileHeader, fileUtil.getBaseFolderName());
                progressModel = new ProgressModel();
                progressModel.setProgress(i);
                progressModel.setFileName(fileHeader.getFileName());
                publishProgress(progressModel);
                if (AppHelper.isRoot()) {
                    Result result = AppHelper.installApkSilently(new File(fileUtil.getBaseFolderName() + fileHeader.getFileName()).getPath());
                    if (result != null && result.getStatusCode() == Result.ResultEnum.INSTALL_SUCCESS.getStatusCode()) {
                        boolean deleteApk = new File(fileUtil.getBaseFolderName() + fileHeader.getFileName()).delete();
                    }
                } else {
                    progressModel.setFilePath(fileUtil.getBaseFolderName() + fileHeader.getFileName());
                }
            }
            zipFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return null;
    }