将多个文件压缩成gzip,将gzip解压成多个文件
来源:互联网 发布:元数据驱动开发 编辑:程序博客网 时间:2024/05/21 11:05
第一步:文件压缩和解压缩方法
//解压gzip文件public static boolean extractZip(File file, File parent) {ZipFile zf = null;try {zf = new ZipFile(file);Enumeration<? extends ZipEntry> entries = zf.entries();if (entries == null)return false;final byte[] buf = new byte[256];while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();if (entry == null)continue;if (entry.isDirectory()) {File dir = new File(parent, entry.getName());dir.mkdirs();continue;}File dstFile = new File(parent, entry.getName());if (!dstFile.exists()) {dstFile.getParentFile().mkdirs();}InputStream fis = zf.getInputStream(entry);BufferedInputStream bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream(dstFile);BufferedOutputStream bos = new BufferedOutputStream(fos);int read = 0;while ((read = bis.read(buf)) > 0) {bos.write(buf, 0, read);}fis.close();bis.close();bos.close();fos.close();}return true;} catch (Exception e) {e.printStackTrace();return false;} finally {try {zf.close();} catch (Exception e) {e.printStackTrace();}}}/** * 对将单个文件进行压缩 * * @param source * 源文件 * @param target * 目标文件 * @throws IOException */public static void zipFile(String source, String target) throws IOException {FileInputStream fin = null;FileOutputStream fout = null;GZIPOutputStream gzout = null;try {fin = new FileInputStream(source);fout = new FileOutputStream(target);gzout = new GZIPOutputStream(fout);byte[] buf = new byte[1024];int num;while ((num = fin.read(buf)) != -1) {gzout.write(buf, 0, num);}} finally {if (gzout != null)gzout.close();if (fout != null)fout.close();if (fin != null)fin.close();}} //多个文件压缩成gzip文件public static void mutileFileToGzip(ArrayList<String> filePaths, String targetFileName) {try {File file = new File(targetFileName);FileOutputStream fout = new FileOutputStream(file);BufferedInputStream bin = null;ZipOutputStream zout = new ZipOutputStream(fout);for (String fileSource : filePaths) {String[] fileNames = fileSource.split("/");zout.putNextEntry(new ZipEntry(fileNames[fileNames.length - 1]));int c;bin = new BufferedInputStream(new FileInputStream(fileSource));while ((c = bin.read()) != -1) {zout.write(c);}bin.close();}zout.close();} catch (Exception e) {e.printStackTrace();}System.out.println("压缩成功!");}
第二步、如何调用?
1、将多个文件打包成gzip:
ArrayList<String> filePaths = new ArrayList<String>();filePaths.add(memeDirectoryName + "/meta.json");imgPath=java.net.URLDecoder.decode(imgPath,"UTF-8"); filePaths.add(imgPath);String targetZipFileName = memeDirectoryName + File.separator + gzipName;targetZipFileName=java.net.URLDecoder.decode(targetZipFileName,"UTF-8"); FileUtils.mutileFileToGzip(filePaths, targetZipFileName);
2、gzip解压成多个文件
FileUtils.extractZip(new File("/Users/figo/Downloads/memepackages/test1/test1.gzip"), new File("/Users/figo/Downloads/memepackages/test1/"));
3、如何获取当前文件所在的路径?
public static String getRealPath() {String realPath = "";try {File file = new File(ReadExcelWriteZip.class.getResource("/").getPath());// 显示该jar所在的路径// String realPath// =ReadExcelWriteZip.class.getClassLoader().getResource("").getFile();realPath = file.getAbsolutePath();realPath=java.net.URLDecoder.decode(realPath,"UTF-8"); } catch (Exception e) {if (e != null) {e.printStackTrace();realPath = e.getMessage();}}return realPath;}
4、如何通过命令行调用jar文件
通过eclipse导出可执行的jar,其实双击这个jar就可以执行里面的Main函数的,不过也可以写一个bat文件,双击后在windows操作系统运行
bat文件这么写: java -jar test.jar
1 0
- 将多个文件压缩成gzip,将gzip解压成多个文件
- Gzip压缩和解压文件工具类
- Java 将文件压缩为GZIP格式
- java 文件压缩 GZIP
- 压缩和解压缩GZIP文件
- Linux下使用gzip压缩与解压文件
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- java中zip与gzip实现文件压缩,解压
- golang tar gzip压缩,解压(含目录文件)
- Java实现文件压缩与解压[zip格式,gzip格式]
- Linux中使用gzip来压缩/解压 *.gz文件
- Linux 文件的解压和压缩 —— gzip命令
- 使用java解压GZip文件
- GZIP文件压缩的应用
- gzip解压和压缩
- Gzip压缩和解压
- android--textview实现文字自适应
- android--No grammar constraints (DTD or XML schema) detected for the document警告
- socket http rpc学习总结
- C语言字符串应用总结[转]
- windows下Eclipse安装Perl插件教程
- 将多个文件压缩成gzip,将gzip解压成多个文件
- objc-十六进制转换成ascii
- SIFT 特征原理与理解
- Hdu 5586 sum【最大连续子序列和】
- objc 类型转换
- objc--int NSUInteger NSInteger NSNumber
- objc--NSUInteger and INT 转换问题
- android+zip4j开源项目问题汇总
- android--系统路径获取