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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.