将多个文件压缩成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
原创粉丝点击