/** * 压缩文件夹 生成压缩文件 * * @author aaa * */ public class ZipFile { /** * * @param filePath * 压缩文件夹的路径 */ public static void zipFile(String filePath) { Log.e("开始压缩文件", System.currentTimeMillis() + ""); String srcPath = "";// 压缩文件的路径 String archPath = "";// 压缩包保存的路径 String[] fileSrcStrings;// 指定压缩源,可以是目录或文件的数组 String commentString = "Android Java Zip 测试.";// 压缩包注释 ZipUtil mZipControl; String path = Environment.getExternalStorageDirectory().getAbsolutePath(); archPath = path + "/testcase"; File zipFile = new File(archPath);// 创建保存zip文件的文件夹 if (!zipFile.exists()) { zipFile.mkdir(); } srcPath = filePath; File srcFile = new File(srcPath);// 创建压缩源的文件夹 if (!srcFile.exists()) { srcFile.mkdir(); } fileSrcStrings = new String[] { srcFile.toString() }; mZipControl = new ZipUtil(); try { mZipControl.writeByApacheZipOutputStream(fileSrcStrings, archPath + "/" + ConfigTest.LOG_FILE_NAME + ".zip", commentString); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 压缩文件和文件夹 * * @param srcFileString 要压缩的文件或文件夹 * @param zipFileString 压缩完成的Zip路径 * @throws Exception */ public static void zipFolder(String srcFileString, String zipFileString,String MP4Name) { //创建ZIP ZipOutputStream outZip = null; try { //创建文件 File file = new File(srcFileString); File outPath = new File(zipFileString); if (!outPath.exists()){ outPath.mkdirs(); } outZip = new ZipOutputStream(new FileOutputStream(new File(outPath, MP4Name+".zip"))); //压缩MP4 ZipFiles(file.getParent() + File.separator, file.getName(), outZip); } catch (Exception e) { e.printStackTrace(); } finally { //完成和关闭 if (outZip != null) { try { outZip.finish(); outZip.close(); }catch (IOException e) { e.printStackTrace(); } } } } /** * 压缩文件 * * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */ private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { if (zipOutputSteam == null) return; FileInputStream inputStream = null; try { File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else { //文件夹 String fileList[] = file.list(); //没有子文件和压缩 if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString + File.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //子文件和递归 for (int i = 0; i < fileList.length; i++) { ZipFiles(folderString + fileString + "/", fileList[i], zipOutputSteam); } } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } public static void noZipFile(String filePath,String lastFileName){ Log.e("开始压缩文件", System.currentTimeMillis() + ""); String srcPath = "";// 压缩文件的路径 String archPath = "";// 压缩包保存的路径 String[] fileSrcStrings;// 指定压缩源,可以是目录或文件的数组 String commentString = "Androi Java Zip 测试.";// 压缩包注释 ZipUtil mZipControl; String path = Environment.getExternalStorageDirectory().getAbsolutePath(); archPath = path + "/testcase"; File zipFile = new File(archPath);// 创建保存zip文件的文件夹 if (!zipFile.exists()) { zipFile.mkdir(); } srcPath = filePath; File srcFile = new File(srcPath);// 创建压缩源的文件夹 if (!srcFile.exists()) { srcFile.mkdir(); } fileSrcStrings = new String[] { srcFile.toString() }; mZipControl = new ZipUtil(); try { mZipControl.writeByApacheZipOutputStream(fileSrcStrings, archPath + "/" + lastFileName + ".zip", commentString); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
压缩文件到指定文件夹工具类
最新推荐文章于 2023-10-30 19:23:38 发布