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

The following examples show how to use net.lingala.zip4j.core.ZipFile#getFileHeaders() . 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 document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
public List<String> listEntries(File zipsource) {
	List<String> files = new ArrayList<String>();
	try {
		ZipFile zipFile = new ZipFile(zipsource);
		setCharset(zipFile);

		@SuppressWarnings("unchecked")
		List<FileHeader> fileHeaders = zipFile.getFileHeaders();
		for (FileHeader fileHeader : fileHeaders) {
			files.add(fileHeader.getFileName());
		}
	} catch (Throwable e) {
		logError(e.getMessage());
	}
	return files;
}
 
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 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 3
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 4
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 5
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 6
Source File: BuildTool.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void expandArtifact(File artifactFile) throws IOException {
    try {
        ZipFile zipFile = new ZipFile(artifactFile);
        for (FileHeader each : (List<FileHeader>) zipFile.getFileHeaders()) {
            String fileName = each.getFileName();
            if (fileName.startsWith("META-INF") && !fileName.startsWith("META-INF/versions")) {
                continue;
            }
            if (each.isDirectory()) {
                continue;
            }
            this.archive.add(new ZipFileHeaderAsset(zipFile, each), fileName);
        }
    } catch (ZipException e) {
        throw new IOException(e);
    }
}
 
Example 7
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 8
Source File: CompressUtil.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 打印zip文件信息
 * 
 * @param path
 */
public static void printZipInfo(String path) {
	try {
		ZipFile zipFile = new ZipFile(path);
		List fileHeaderList = zipFile.getFileHeaders();
		for (int i = 0; i < fileHeaderList.size(); i++) {
			FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
			// FileHeader包含zip文件中对应文件的很多属性
			System.out.println("****File Details for: " + fileHeader.getFileName() + "*****");
			System.out.println("文件名: " + fileHeader.getFileName());
			System.out.println("是否为目录: " + fileHeader.isDirectory());
			System.out.println("压缩后大小: " + fileHeader.getCompressedSize());
			System.out.println("未压缩大小: " + fileHeader.getUncompressedSize());
			System.out.println("************************************************************");
		}
	} catch (ZipException e) {
		e.printStackTrace();
	}

}
 
Example 9
Source File: WorldHandler.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
/**
 * Tries to load the map data for a name
 *
 * @param name the name of the map to load
 * @return the loaded map
 * @throws MapException when
 */
@Nonnull
public Map loadMap(@Nonnull String name) {
    Optional<Map> map = getMap(name);
    if (!map.isPresent()) {
        Optional<MapInfo> mapInfo = getMapInfo(name);
        if (!mapInfo.isPresent()) {
            throw new MapException(
                    "Unknown map " + name + ". Did you register it into the world config?");
        }

        try {
            ZipFile zipFile = new ZipFile(new File(worldsFolder, mapInfo.get().getWorldName() + ".zip"));
            for (FileHeader header : (List<FileHeader>) zipFile.getFileHeaders()) {
                if (header.getFileName().endsWith("config.json")) {
                    InputStream stream = zipFile.getInputStream(header);
                    Map m = gson.fromJson(new JsonReader(new InputStreamReader(stream)), Map.class);
                    m.initMarkers(mapHandler);
                    maps.add(m);
                    return m;
                }
            }
            throw new MapException("Could not load map config for map " + name
                    + ". Fileheader was null. Does it has a map.json?");

        } catch (Exception e) {
            throw new MapException("Error while trying to load map config " + name + "(" + mapInfo.get().getWorldName() + ".zip)", e);
        }
    } else {
        return map.get();
    }
}
 
Example 10
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);
    }
 
Example 11
Source File: CompressUtil.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 使用给定密码解压指定的ZIP压缩文件到指定目录
 * <p>
 * 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出
 * 
 * @param zip
 *            指定的ZIP压缩文件
 * @param dest
 *            解压目录
 * @param passwd
 *            ZIP文件的密码
 * @param charset
 *            ZIP文件的编码
 * @return 解压后文件数组
 * @throws ZipException
 *             压缩文件有损坏或者解压缩失败抛出
 */
public static File[] unzip(File zipFile, String dest, String passwd,
		String charset) throws ZipException {
	ZipFile zFile = new ZipFile(zipFile);
	if (null != charset || !"".equals(charset)) {
		zFile.setFileNameCharset(charset);
	}
	if (!zFile.isValidZipFile()) {
		throw new ZipException("压缩文件不合法,可能被损坏.");
	}
	File destDir = new File(dest);
	if (destDir.isDirectory() && !destDir.exists()) {
		destDir.mkdir();
	}
	if (zFile.isEncrypted()) {
		zFile.setPassword(passwd.toCharArray());
	}
	zFile.extractAll(dest);

	List<FileHeader> headerList = zFile.getFileHeaders();
	List<File> extractedFileList = new ArrayList<File>();
	for (FileHeader fileHeader : headerList) {
		if (!fileHeader.isDirectory()) {
			extractedFileList.add(new File(destDir, fileHeader
					.getFileName()));
		}
	}
	File[] extractedFiles = new File[extractedFileList.size()];
	extractedFileList.toArray(extractedFiles);
	return extractedFiles;
}
 
Example 12
Source File: CompressUtil.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 删除压缩文件中的目录
 * 
 * @param file
 *            目标zip文件
 * @param removeDir
 *            要移除的目录
 * @param charset
 *            编码
 * @throws ZipException
 */
@SuppressWarnings("rawtypes")
private static void removeDirFromZipArchive(String file, String removeDir,
		String charset) throws ZipException {
	// 创建ZipFile并设置编码
	ZipFile zipFile = new ZipFile(file);

	if (null != charset || !"".equals(charset)) {
		zipFile.setFileNameCharset(charset);
	}

	// 给要删除的目录加上路径分隔符
	if (!removeDir.endsWith(File.separator))
		removeDir += File.separator;

	// 如果目录不存在, 直接返回
	FileHeader dirHeader = zipFile.getFileHeader(removeDir);
	if (null == dirHeader)
		return;

	// 遍历压缩文件中所有的FileHeader, 将指定删除目录下的子文件名保存起来
	List headersList = zipFile.getFileHeaders();
	List<String> removeHeaderNames = new ArrayList<String>();
	for (int i = 0, len = headersList.size(); i < len; i++) {
		FileHeader subHeader = (FileHeader) headersList.get(i);
		if (subHeader.getFileName().startsWith(dirHeader.getFileName())
				&& !subHeader.getFileName().equals(dirHeader.getFileName())) {
			removeHeaderNames.add(subHeader.getFileName());
		}
	}
	// 遍历删除指定目录下的所有子文件, 最后删除指定目录(此时已为空目录)
	for (String headerNameString : removeHeaderNames) {
		zipFile.removeFile(headerNameString);
	}
	zipFile.removeFile(dirHeader);
}
 
Example 13
Source File: FileIO.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
static public List seeZipContent(String file) throws ZipException {
    ZipFile zipFile = new ZipFile(file);
    List list = zipFile.getFileHeaders();
    return list;
}
 
Example 14
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;
    }