package com.bootdo.common.utils; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.zip.InflaterInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import com.mongodb.gridfs.GridFSDBFile; public class ZipUtils { public static void main(String[] a) { } /** * 读取整个zip * * @throws IOException */ public static void readZipContext() throws IOException { String zipPath = "D:\\17340df9-d3a6-43c6-a3b9-d89de8829c85.zip"; ZipFile zf = new ZipFile(zipPath); InputStream in = new BufferedInputStream(new FileInputStream(zipPath)); ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // 为空的文件夹什么都不做 } else { if (ze.getSize() > 0) { BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), "utf-8")); String line = null; while ((line = reader.readLine()) != null) { } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } in.close(); zin.close(); } /** * 获得一个文件的string格式的输出 * * @param filename * @param zipName * @return * @throws IOException */ public static InputStream getFileInZip(String filename, InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String rline = ""; File rfile; ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; InputStream is = null; // ZipOutputStream zos = null; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // 为空的文件夹什么都不做 } else { if (filename.equals(ze.toString())) { byte[] data = getByte(zin); // 获取当前条目的字节数组 is = new ByteArrayInputStream(data); // 把当前条目的字节数据转换成Inputstream流 } } } /* return rline; */ in.close(); baos.close(); return is; } /** * 获得zip文件下所有文件名 * * @return * @throws IOException */ public static List<String> getAllFileNameInStreamZip(InputStream in) throws IOException { List<String> fileNameList = new LinkedList<String>(); ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { } else { fileNameList.add(ze.getName()); } } in.close(); zin.close(); return fileNameList; } public static String getHtmlStrByName(String fileName, String zipName) { return null; } /** * 字符串转换成流 * * @param sInputString * @return */ public static InputStream getStringStream(String sInputString) { if (sInputString != null && !sInputString.trim().equals("")) { try { ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes()); return tInputStringStream; } catch (Exception ex) { ex.printStackTrace(); } } return null; } /** * inputstream转字符串 * * @param tInputStream * @return */ public static String getStreamString(InputStream tInputStream) { if (tInputStream != null) { try { BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(tInputStream)); StringBuffer tStringBuffer = new StringBuffer(); String sTempOneLine = new String(""); while ((sTempOneLine = tBufferedReader.readLine()) != null) { tStringBuffer.append(sTempOneLine); } return tStringBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); } } return null; } /** * 输入流转成文件 * * @param ins * @param file * @throws IOException */ public static void inputstreamtofile(InputStream ins, File file) throws IOException { OutputStream os; try { os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 获取到zip文件下所有文件的输入流 * * @param fileName * @param zipName * @return * @throws IOException */ public static Map<String, InputStream> getFileInputStreamInZip(String zipName) throws IOException { String zipPath = "D:\\" + zipName; ZipFile zf = new ZipFile(zipPath);// 找到zip文件 Map<String, InputStream> map = new HashMap<String, InputStream>(); InputStream in = new BufferedInputStream(new FileInputStream(zipPath));// 找到zip文件输入流 ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { } else { try { InputStream i = zf.getInputStream(ze); map.put(ze.getName(), i); } catch (IOException e) { e.printStackTrace(); } } } in.close(); zin.close(); return map; } /** * 根据文件名,返回文件的输入流 * * @param filename * @param zipName * @return * @throws IOException */ public static InputStream getFileInputStreamByFileName(String filename, InputStream in) throws IOException { String rline = ""; File rfile; InputStream i = null; ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // 为空的文件夹什么都不做 } else { if (filename.equals(ze.toString())) { i = in; } } } zin.close(); return i; } /** * 向file文件写入字节 * * @param ins * @param file */ public static void toWrite(InputStream ins, File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取条目byte[]字节 * * @param zis * @return */ public static byte[] getByte(InflaterInputStream zis) { try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] temp = new byte[1024]; byte[] buf = null; int length = 0; while ((length = zis.read(temp, 0, 1024)) != -1) { bout.write(temp, 0, length); } buf = bout.toByteArray(); bout.close(); return buf; } catch (IOException e) { e.printStackTrace(); return null; } } /** * inputStream转String * * @param is * @return */ public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } try { reader.close(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /** * 预览时将文件保存在本地 * * @param gfs * @param dirName * @param zipInLocal */ public static void saveToLocal(GridFSDBFile gfs, String dirName, File zipInLocal) { InputStream in = gfs.getInputStream(); FileUtil.createDir(dirName); try { inputstreamtofile(in, zipInLocal); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 获得一个文件的string格式的输出 * * @param filename * @param zipName * @return * @throws IOException */ public static String getFileInLocalZip(String filename, String zipPathName) throws IOException { String rline = ""; ZipFile zf = new ZipFile(zipPathName); File rfile; FileInputStream fileIn = new FileInputStream(zipPathName); InputStream in = new BufferedInputStream(fileIn); ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // 为空的文件夹什么都不做 } else { if (filename.equals(ze.toString())) { BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), "utf-8")); String line = ""; while ((line = reader.readLine()) != null) { rline = rline + line; } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } fileIn.close(); zin.close(); in.close(); return rline; } /** * 读取本地文件zip中所有文件名字 * * @return * @throws IOException */ public static List<String> getAllFileNameInLocalZip(String zipPath) throws IOException { ZipFile zf = new ZipFile(zipPath); List<String> fileNameList = new LinkedList<String>(); FileInputStream fileIn = new FileInputStream(zipPath); InputStream in = new BufferedInputStream(fileIn); ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { } else { fileNameList.add(ze.getName()); } } fileIn.close(); zin.close(); in.close(); return fileNameList; } /** * 根据文件名,返回文件的输入流 * * @param filename * @param zipPath * @return * @throws IOException */ public static InputStream getLocalFileInputStreamByFileName(String filename, String zipPath) throws IOException { ZipFile zf = new ZipFile(zipPath); InputStream in = new BufferedInputStream(new FileInputStream(zipPath)); InputStream i = null; ZipInputStream zin = new ZipInputStream(in); // ZipEntry 类用于表示 ZIP 文件条目。 ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // 为空的文件夹什么都不做 } else { if (filename.equals(ze.toString())) { i = zf.getInputStream(ze); } } } in.close(); return i; } /** * 打包文件 * * @param subs * 文件数组 * @param baseName * 自定义名字 * @param zos * 输出流 * @throws IOException */ public static void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException { for (int i = 0; i < subs.length; i++) { File f = subs[i]; zos.putNextEntry(new ZipEntry(baseName + f.getName())); FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[1024]; int r = 0; while ((r = fis.read(buffer)) != -1) { zos.write(buffer, 0, r); } fis.close(); } } /** * 压缩包名字 * * @return */ public static String getZipFilename() { Date date = new Date(); String s = date.getTime() + ".zip"; return s; } private static final String DEFAULT_ENCODING = "GBK";// 编码 public static String readInfoStream(InputStream input) throws Exception { if (input == null) { throw new Exception("输入流为null"); } // 字节数组 byte[] bcache = new byte[2048]; int readSize = 0;// 每次读取的字节长度 int totalSize = 0;// 总字节长度 ByteArrayOutputStream infoStream = new ByteArrayOutputStream(); try { // 一次性读取2048字节 while ((readSize = input.read(bcache)) > 0) { totalSize += readSize; // 将bcache中读取的input数据写入infoStream infoStream.write(bcache, 0, readSize); } } catch (IOException e1) { throw new Exception("输入流读取异常"); } finally { try { // 输入流关闭 input.close(); } catch (IOException e) { throw new Exception("输入流关闭异常"); } } try { return infoStream.toString(DEFAULT_ENCODING); } catch (UnsupportedEncodingException e) { throw new Exception("输出异常"); } } }
zipUtil
最新推荐文章于 2024-05-15 10:44:12 发布