解压工具类
? ? /**
? ? ?* 解壓文件,不設置密碼
? ? ?* @param zipFilePath 被解壓的文件路徑(完整路徑)
? ? ?* @param unzipFilePath 解壓后文件保存的路徑 (文件的輸出路徑)
? ? ?*/
? ? public static void unZip (String zipFilePath , String unzipFilePath) {
? ? ? ? unZip(zipFilePath , unzipFilePath , "");
? ? }
? ? /**
? ? ?* 解壓文件,支持中文路徑已經文件名
? ? ?* @param zipFilePath 壓縮文件路徑(完整路徑)
? ? ?* @param unzipFilePath 解壓后輸出的路徑(文件的輸出路徑)
? ? ?* @param password 解壓密碼
? ? ?*/
? ? public static void unZip (String zipFilePath , String unzipFilePath , String password) {
? ? ? ? try {
? ? ? ? ? ? File zipFile = new File(zipFilePath);
? ? ? ? ? ? if(zipFile.exists()) {
? ? ? ? ? ? ? ? net.lingala.zip4j.core.ZipFile zip4j = new net.lingala.zip4j.core.ZipFile(zipFile);
? ? ? ? ? ? ? ? if(zip4j.isEncrypted() && !TextUtils.isEmpty(password)) {
? ? ? ? ? ? ? ? ? ? zip4j.setPassword(password);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? zip4j.extractAll(unzipFilePath);
? ? ? ? ? ? }
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /**
? ? ?* @param context
? ? ?* @param assertName
? ? ?* @return
? ? ?*/
? ? public static boolean unzipAssetsZip(Context context, String assertName, String fromPath) {
? ? ? ? File file = new File(fromPath);
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? file.createNewFile();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? InputStream in = context.getAssets().open(assertName + ".zip");
? ? ? ? ? ? boolean success = unpackZip(in, file);
? ? ? ? ? ? return success;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? LogUtils.e("讀取AssetsZip解壓======"+e.getMessage());
? ? ? ? }
? ? ? ? return false;
? ? }
? ? private static boolean unpackZip(InputStream in, File outputDir) {
? ? ? ? if (outputDir.exists()) {
? ? ? ? ? ? deleteFileAndDir(outputDir);
? ? ? ? }
? ? ? ? outputDir.mkdirs();
? ? ? ? ZipInputStream zis;
? ? ? ? try {
? ? ? ? ? ? String filename;
? ? ? ? ? ? zis = new ZipInputStream(new BufferedInputStream(in));
? ? ? ? ? ? ZipEntry ze;
? ? ? ? ? ? byte[] buffer = new byte[1024];
? ? ? ? ? ? int count;
? ? ? ? ? ? while ((ze = zis.getNextEntry()) != null) {
? ? ? ? ? ? ? ? // zapis do souboru
? ? ? ? ? ? ? ? filename = ze.getName();
? ? ? ? ? ? ? ? if (ze.isDirectory()) {
? ? ? ? ? ? ? ? ? ? //zipEntry是目錄,則創建目錄
? ? ? ? ? ? ? ? ? ? filename = filename.substring(0, filename.length() - 1);
? ? ? ? ? ? ? ? ? ? File folder = new File(outputDir, filename);
? ? ? ? ? ? ? ? ? ? folder.mkdirs();
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //否則創建文件,并輸出文件的內容
? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(filename) && filename.contains("/")) {
? ? ? ? ? ? ? ? ? ? String tepFileName = filename.substring(0, filename.lastIndexOf('/'));
? ? ? ? ? ? ? ? ? ? File folder = new File(outputDir, tepFileName);
? ? ? ? ? ? ? ? ? ? folder.mkdirs();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? File file = new File(outputDir + File.separator + filename);
? ? ? ? ? ? ? ? file.createNewFile();
? ? ? ? ? ? ? ? FileOutputStream fout = new FileOutputStream(file);
? ? ? ? ? ? ? ? while ((count = zis.read(buffer)) != -1) {
? ? ? ? ? ? ? ? ? ? fout.write(buffer, 0, count);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? fout.close();
? ? ? ? ? ? ? ? zis.closeEntry();
? ? ? ? ? ? }
? ? ? ? ? ? zis.close();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? LogUtils.d("解壓文件失敗====="+e.getMessage());
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? LogUtils.d("解壓文件成功:" + outputDir.getAbsolutePath());
? ? ? ? return true;
? ? }
? ? private static void deleteFileAndDir(File file) {
? ? ? ? if (file.isDirectory()) {
? ? ? ? ? ? File[] subFiles = file.listFiles();
? ? ? ? ? ? for (File subFile : subFiles) {
? ? ? ? ? ? ? ? deleteFileAndDir(subFile);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? file.delete();
? ? }
? ? /**
? ? ?* 壓縮文件,無密碼壓縮
? ? ?* @param zipFilePath 壓縮后zip包的存儲路徑? (完整路徑)
? ? ?* @param rawfilePatn 被壓縮的文件? (文件的輸出路徑)
? ? ?*/
? ? public static void Zip (String zipFilePath , String rawfilePatn ) {
? ? ? ? Zip(zipFilePath , rawfilePatn , "");
? ? }
? ? /**
? ? ?* 壓縮一個文件
? ? ?* @param zipFilePath 壓縮存儲的路徑 (完整路徑)
? ? ?* @param rawfilePatn 被壓縮的文件 (文件的輸出路徑)
? ? ?* @param password 壓縮密碼
? ? ?*/
? ? public static void Zip (String zipFilePath , String rawfilePatn , String password) {
? ? ? ? try {
? ? ? ? ? ? // 創建一個zip包
? ? ? ? ? ? net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(zipFilePath);
? ? ? ? ? ? ArrayList<File> fileAddZip = new ArrayList<File>();? ?// 向zip包中添加文件集合
? ? ? ? ? ? fileAddZip.add(new File(rawfilePatn));? ? ? ? ? ? ? ? // 向zip包中添加一個pdf文件
? ? ? ? ? ? ZipParameters parameters = new ZipParameters(); // 設置zip包的一些參數集合
? ? ? ? ? ? if(!TextUtils.isEmpty(password)) {
? ? ? ? ? ? ? ? parameters.setEncryptFiles(true); // 是否設置密碼(此處設置為:是)
? ? ? ? ? ? ? ? parameters.setPassword(password); // 壓縮包密碼
? ? ? ? ? ? }
? ? ? ? ? ? parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);? ? ? ? // 壓縮方式(默認值)
? ? ? ? ? ? parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通級別(參數很多)
? ? ? ? ? ? parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);? // 加密級別
? ? ? ? ? ? zipFile.createZipFile(fileAddZip , parameters);? ? ? ?// 創建壓縮包完成
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
總結
- 上一篇: 如何正确地选择和使用家居装饰品?
- 下一篇: 维朗门窗怎么样 详解维朗门窗的品质与服务