由于频繁需要解压ZIP,加压ZIP
故自己写了个工具类方便以后处理时用到,暂时不给过多解释,有啥问题,大家一起讨论哈
FileItem:
public class FileItem { private String fileName; private byte[] fileContent; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public byte[] getFileContent() { return fileContent; } public void setFileContent(byte[] fileContent) { this.fileContent = fileContent; } }
FileUtil:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class FileUtil { /**缓存大小*/ private final static int BUFFER_SIZE = 8192; /**后缀名*/ private final static String SUFFIX = "zip"; /**后缀名分隔符*/ private final static String POINT = "."; /** * 获得字节数组 * * @param filepath 文件全路径 * @return 字节数组 * @throws IOException IOException */ public static byte[] getBytes(File file) throws IOException{ return getBytes(file.getAbsolutePath()); } public static byte[] getBytes(String filepath) throws IOException { BufferedInputStream bis = null; ByteArrayOutputStream byteArrayOutputStream = null; BufferedOutputStream bos = null; try { byteArrayOutputStream = new ByteArrayOutputStream(); bis = new BufferedInputStream(new FileInputStream(filepath)); bos = new BufferedOutputStream(byteArrayOutputStream); byte[] bytes = new byte[BUFFER_SIZE]; int len; while ((len = bis.read(bytes, 0, BUFFER_SIZE)) != -1) { bos.write(bytes, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); throw e; } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (null != bis) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != byteArrayOutputStream) { try { byteArrayOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return byteArrayOutputStream.toByteArray(); } /** * 统一文件分隔符为LINUX,类UNIX文件系统风格 * * @param direcotory 目录 * @return 统一处理后目录字符串风格 */ private static String replaceAllSeparator(String direcotory) { String str = direcotory.replaceAll("\\\\", "/"); return str.length() != str.lastIndexOf("/") ? str + "/" : str; } /** * 将文件项写到文件 * * @param fileItem 文件项 * @param directory 目录 * @throws IOException IOException */ public static void writeFile(FileItem fileItem, String directory) throws IOException { File dir = new File(directory); if (!dir.exists()) { dir.mkdirs(); } writeFile(fileItem.getFileContent(), replaceAllSeparator(directory) + fileItem.getFileName()); } /** * 写文件 * * @param fileContent 文件二进制流 * @param filepath 文件全路径 * @throws IOException IOException */ public static void writeFile(byte[] fileContent, String filepath) throws IOException { InputStream inputStream = new ByteArrayInputStream(fileContent); writeFile(inputStream, filepath); } /** * 将流写到一个文件 * * @param inputStream 文件流 * @param filepath 文件路径 * @throws IOException IOException */ public static void writeFile(InputStream inputStream, String filepath) throws IOException { BufferedOutputStream bos = null; BufferedInputStream bis = null; try { File file = new File(filepath); if (!file.exists()) { file.createNewFile(); } bos = new BufferedOutputStream(new FileOutputStream(file)); bis = new BufferedInputStream(inputStream); byte[] buffer = new byte[BUFFER_SIZE]; int len = -1; while ((len = bis.read(buffer, 0, BUFFER_SIZE)) != -1) { bos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (null != bos) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } if (null != bis) try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 解压zip文件 * @param filepath 文件全路径 * @return 解压后的文件项 * @throws IOException IOException */ public static List uncompressZip(String filepath) throws IOException { ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; ZipInputStream zis = null; // zipInputStream List fileList = null; try { zis = new ZipInputStream(new FileInputStream(filepath)); baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos); fileList = new ArrayList(); byte[] buffer = new byte[BUFFER_SIZE]; int len = -1; ZipEntry zipEntry = null; while (null != (zipEntry = zis.getNextEntry())) { if (null != zipEntry) { System.out.println("Name:" + zipEntry.getName() + ",Size:" + zipEntry.getSize() + "bytes"); while (-1 != (len = zis.read(buffer))) { bos.write(buffer, 0, len); } bos.flush(); byte[] fileContent = baos.toByteArray(); FileItem item = new FileItem(); item.setFileName(zipEntry.getName()); item.setFileContent(fileContent); fileList.add(item); } } } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (null != zis) { try { zis.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != baos) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileList; } /** * 将目录压缩成zip包 * * @param filepath * 文件全路径 * @throws IOException * IOException */ public static FileItem compressZip(String filepath) throws IOException { File directory = new File(filepath); String filename = directory.getName(); File[] fileList = directory.listFiles(); ZipOutputStream zos = null; BufferedInputStream bis = null; ByteArrayOutputStream baos = null; FileItem fileItem = null; try { baos = new ByteArrayOutputStream(); zos = new ZipOutputStream(baos); byte[] buffer = new byte[BUFFER_SIZE]; int len = -1; for (File file : fileList) { bis = new BufferedInputStream(new FileInputStream(file)); while (-1 != (len = bis.read(buffer, 0, BUFFER_SIZE))) { //ZipEntry为Zip压缩文件的每一项 ZipEntry zipEntry = new ZipEntry(file.getName()); zipEntry.setSize(file.length()); zipEntry.setCompressedSize(file.length()); zos.putNextEntry(zipEntry); zos.write(buffer, 0, len); } //做完一个文件的读写应清下缓存 zos.flush(); if (null != bis) { bis.close(); } } //完成所有文件的读取,转化为一个二进制流的包装zos zos.finish(); fileItem = new FileItem(); int index = filename.lastIndexOf("."); filename = filename.substring(0, -1 == index ? filename.length() : index); //要生成zip文件的文件名 fileItem.setFileName(filename + POINT + SUFFIX); //要生成的zip文件的二进制流 fileItem.setFileContent(baos.toByteArray()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (null != baos) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != bis) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != zos) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileItem; } /** * 解压zip文件 * * @param zipFile zip文件 * @return 解压后的文件项 * @throws IOException IOException */ public static List uncompressZip(File zipFile) throws IOException { String filepath = zipFile.getAbsolutePath(); return uncompressZip(filepath); } public static void main(String[] args) throws IOException { //加压 FileItem fileItem = compressZip("F:/Hbzspt/sbinput/SCYSB_1234567890_0901_20100912082719250"); //将SCYSB_1234567890_0901_20100912082719250.zip文件写到F:/zjie目录 FileUtil.writeFile(fileItem, "F:/zjie"); //解压F:/zjie/SCYSB_1234567890_0901_20100912082719250.zip文件 List fileItemList = FileUtil .uncompressZip("F:/zjie/SCYSB_1234567890_0901_20100912082719250.zip"); //将解压后的文件流生成文件写到F:/zjie/SCYSB_1234567890_0901_20100912082719250/目录 for (int i = 0; i < fileItemList.size(); i++) { FileItem item = (FileItem) fileItemList.get(i); DbfUtil.dbfReader(item.getFileContent()); FileUtil.writeFile(item.getFileContent(), "F:/zjie/SCYSB_1234567890_0901_20100912082719250/"+item.getFileName()); } } }