- 浏览: 195317 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
se34218:
我想问一下,为什么插入CLOB字段的时候要先插入空CLOB,e ...
利用JDBC操作Oracle CLOB和BLOB类型数据 -
tinger1:
狂赞楼主!
GWT通信机制初探 -
skygege20:
2015-7-24 16:09:14 org.apache.c ...
Java中禁止的包名(Prohibited package name) -
skygege20:
那怎么解决这种问题呢?
Java中禁止的包名(Prohibited package name) -
javashiting:
看看 看看 看看 看看
Struts+Hibernate/JDBC+ExtJS实现人力资源管理系统(一)
压缩需求:压缩文件夹下所有具有统一文件名规范的普通文件,同时添加文件名与压缩包相同的MANIFEST文件,包含所有被压缩文件的文件名及大小列表。
Java文件压缩涉及到的类均在java.uti.zip包下:
代码如下:
解压缩需求:解压缩一个zip文件,读取MANIFEST文件,一一验证MANIFEST文件中所列的文件名和大小,只有当所有文件和大小均匹配时解压zip包至特定目录。
Java文件解压缩需要用到的类包括ZipFile(用文件名实例化一个压缩包)和ZipEntry,ZipEntry实例被从ZipFile实例获取。 利用ZipFile的getOutputStream()(zipEntry实例作为参数)可以获取对zipEntry的输入流,由此读入数据并写出到文件系统。
代码如下:
由于需求特定,代码实现有所局限。
看到Snowolf对带有压缩算法的压缩/解压缩的研究,在实际应用中需要面对不同的问题和场景,如文件读写同步/目录压缩及解压缩/中文编码等。故本文章也会适时更新。
Java文件压缩涉及到的类均在java.uti.zip包下:
- ZipOutputStream: Zip包的输出流,利用putNextEntry()向包内添加一个文件。
- ZipEntry: 将被压缩到包内的文件实例,在文件名后添加文件分隔符可以压缩目录。
代码如下:
public class FileZipper { private static String INPUT_FPATH = "C:/temp/fileZipper/DropZone/Input_Min"; private static String OUTPUT_ZIP_PATH = "C:/temp/fileZipper/DropZone/Input_Zip"; private static String OUTPUT_ZIP_TSPATH = "/SRE/apps/ican505/sandbox/jerry/filezipper/Output_TS"; private static String ARCHIEVE_ZONE = "C:/temp/fileZipper/ArchiveZone"; private static String INPUT_DELIM = ":"; private static String ZIP_FPATTERN = "I2001_GLB"; private static int AVG_FILENAME_LEN = 45; // Default to length of each entry in MANIFEST private static int BUF_SIZE = 1024; private static int STD_ZIP_ENTRIES = 20; public String zipFilesByJava(String strZipFilePattern, String strFilePath, String strOutputFileList, String strErrorZonePath) { System.out.println("***** Inside zipFilesByJava method *****"); System.out.println("***** Source Location of files: " + strFilePath + ", Zip File Pattern: " + strZipFilePattern + " *****"); String strZipFName = ""; String strEntryFName = ""; String strManifestFName = ""; SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmssSS"); StringTokenizer strFileList = new StringTokenizer(strOutputFileList, "|"); if (strFileList == null || strFileList.countTokens() == 0) { System.out.println("[ERROR] Input file list is NULL or EMPTY!"); return null; } FileInputStream fin = null; ZipOutputStream zos = null; ZipEntry zipEntry = null; StringBuffer sbZippedEntryList = null; if (STD_ZIP_ENTRIES <= 0) { System.out.println("[WARN] The property STD_ZIP_ENTRIES can NOT be negative or zero! Reset to 100"); STD_ZIP_ENTRIES = 100; } if (BUF_SIZE <= 0) { System.out.println("[WARN] The property BUF_SIZE can NOT be negative or zero! Reset to 1024"); BUF_SIZE = 1024; } byte[] bInputBuf = new byte[BUF_SIZE]; int iTotalFiles = strFileList.countTokens(); int iTotalZips = iTotalFiles / STD_ZIP_ENTRIES == 0 ? 1 : iTotalFiles / STD_ZIP_ENTRIES; int iEntryListLen = (iTotalZips == 1) ? AVG_FILENAME_LEN : STD_ZIP_ENTRIES * AVG_FILENAME_LEN; int AVG_FILENAME_LEN = 45; // Length of entry in MANIFEST which contains standard filename and file size. // i.e, I1001_GWM_7360_MAN_20100426000000.txt|1111 StringBuffer sbZipFNameList = new StringBuffer(iTotalZips); String strTS = dateFormatter.format(new Date()); try { System.out.println("[INFO] Current Time in Millis: " + System.currentTimeMillis()); for (int iCntZip = 0; iCntZip < iTotalZips; iCntZip++) { if (iTotalFiles == 1) { strZipFName = strOutputFileList; strZipFName = OUTPUT_ZIP_PATH + File.separatorChar + strZipFName.replaceAll(".txt$", ".zip"); } else { strZipFName = OUTPUT_ZIP_PATH + File.separatorChar + strZipFilePattern + "_" + strTS + ".zip"; } zos = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(strZipFName), new CRC32())); if (zos == null) { System.out.println("[ERROR] ZIPOutputStream for output zip file is NULL!"); return null; } if (iCntZip != 0 && iCntZip == iTotalZips - 1) iEntryListLen = STD_ZIP_ENTRIES * 2 * AVG_FILENAME_LEN; sbZippedEntryList = new StringBuffer(iEntryListLen); int iCntEntry = 0; int iReadSize = 0; int iFileSize = 0; while ((iCntZip != iTotalZips - 1 && iCntEntry < STD_ZIP_ENTRIES) || (iCntZip == iTotalZips - 1 && iCntEntry < 2 * STD_ZIP_ENTRIES) && strFileList.hasMoreTokens()) { strEntryFName = strFileList.nextToken(); if (!(new File(strFilePath + File.separatorChar + strEntryFName).exists())) { System.out.println("[ERROR] The input file " + strFilePath + File.separatorChar + strEntryFName + " does NOT exist!"); return null; } zipEntry = new ZipEntry(strEntryFName); zos.putNextEntry(zipEntry); fin = new FileInputStream(strFilePath + File.separatorChar + strEntryFName); while ((iReadSize = fin.read(bInputBuf)) != -1) { zos.write(bInputBuf); iFileSize += iReadSize; bInputBuf = new byte[BUF_SIZE]; } iCntEntry++; sbZippedEntryList.append(strEntryFName + "|" + iFileSize + "\n"); iReadSize = 0; iFileSize = 0; System.out.println("[INFO] Entry No. " + iCntEntry + ": " + strEntryFName); fin.close(); if(new File(strFilePath + File.separatorChar + strEntryFName).renameTo(new File(ARCHIEVE_ZONE + File.separatorChar + strEntryFName))) System.out.println("[INFO] Entry file " + strEntryFName + " is already moved to Archieve Zone!"); } strManifestFName = strZipFilePattern + "_" + strTS + ".MANIFEST"; zipEntry = new ZipEntry(strManifestFName); zos.putNextEntry(zipEntry); zos.write(sbZippedEntryList.toString().getBytes()); zos.close(); System.out.println("---------------------------\n[TOTAL] Zip No. " + (iCntZip + 1) + ": " + iCntEntry + " files compressed into zip " + strZipFName + "!\n---------------------------\n"); sbZipFNameList.append(strZipFName); } System.out.println("[END] Finished reading and zipping!"); System.out.println("[INFO] Current Time in Millis: " + System.currentTimeMillis()); } catch(ZipException zipE){ System.out.println("[ERROR] ZipException thrown!"); zipE.printStackTrace(); } catch (IOException ioE) { System.out.println("[ERROR] IOException thrown!"); ioE.printStackTrace(); } return sbZipFNameList.toString(); }
解压缩需求:解压缩一个zip文件,读取MANIFEST文件,一一验证MANIFEST文件中所列的文件名和大小,只有当所有文件和大小均匹配时解压zip包至特定目录。
Java文件解压缩需要用到的类包括ZipFile(用文件名实例化一个压缩包)和ZipEntry,ZipEntry实例被从ZipFile实例获取。 利用ZipFile的getOutputStream()(zipEntry实例作为参数)可以获取对zipEntry的输入流,由此读入数据并写出到文件系统。
代码如下:
public class FileUnzipper { public static Logger logger = Logger.getLogger(FileUnzipper.class.getName()); public String strErrMsg = ""; void unzip(String strZipFilePath, String strTargetFilePath, String strErrorZone, String strZipPattern, String strClaimsFilePattern, String strArchivePath) { if (!new File(strZipFilePath).exists() || !new File(strTargetFilePath).exists() || strZipPattern == null || strZipPattern.trim().length() <= 0 || strClaimsFilePattern == null || strClaimsFilePattern.trim().length() <= 0){ strErrMsg = "The mandatory location/filename for unzipping is(are) NOT valid!"; logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); return; } File[] fZipArray = new File(strZipFilePath).listFiles(new FileListFilter(strZipPattern, "zip")); if (fZipArray == null || fZipArray.length == 0){ strErrMsg = "No Zip file with pattern " + strZipPattern + " exist in the source path: " + strZipFilePath; logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); return; } ZipFile zipFile = null; ZipEntry zipEntry = null; Enumeration zipAllEntries = null; BufferedReader bufManifestReader = null; Map mapFNameToSize = new HashMap(); InputStream isEntryFile = null; OutputStream osEntryFile = null; OutputStream osEntryArchive = null; String strZipFName = ""; String strManifestFName = ""; String strReadLine = ""; String strEntryFName = ""; long lEntryFSize = 0L; int iEntryCounter = 0; byte[] bReadBuf = new byte[PropSet.FILE_WRITE_BUFSIZE]; boolean bEntryMismatched = false; try{ for (int iZip = 0; iZip < fZipArray.length; iEntryCounter = 0, iZip++){ strZipFName = fZipArray[iZip].getName(); strManifestFName = strZipFName.substring(0, strZipFName.indexOf(".zip")) + ".MANIFEST"; if(!fZipArray[iZip].isFile()){ logger.warn("The zip file " + strZipFName + " is directory or NOT a normal zip file! Moving to ErrorZone..."); if(fZipArray[iZip].renameTo(new File(strErrorZone + File.separator + strZipFName))) logger.info("Successfully moved to ErrorZone!"); else logger.error("Failed to move to ErrorZone!"); continue; } zipFile = new ZipFile(fZipArray[iZip]); // process manifest entry zipEntry = zipFile.getEntry(strManifestFName); if(zipEntry == null){ strErrMsg = "The zip file " + strZipFName + " does NOT contain MANIFEST file! Moving the zip file to ErrorZone..."; logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); if(!fZipArray[iZip].renameTo(new File(strErrorZone + File.separator + strZipFName))) logger.info("Successfully moved to ErrorZone!"); else logger.error("Failed to move to ErrorZone!"); continue; } bufManifestReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry))); while((strReadLine = bufManifestReader.readLine()) != null){ try{ mapFNameToSize.put(strReadLine.split("[|]")[0], strReadLine.split("[|]")[1]); }catch(Exception exc){ throw new Exception("Failed to add [filename --> filesize] mapping to hashmap from manifest file! Line supposed to be mapped: " + strReadLine); } } mapFNameToSize.put(strManifestFName, Long.toString(zipEntry.getSize())); bufManifestReader.close(); // process data entries zipAllEntries = zipFile.entries(); while(zipAllEntries.hasMoreElements()){ bEntryMismatched = false; zipEntry = (ZipEntry)zipAllEntries.nextElement(); strEntryFName = zipEntry.getName().trim(); lEntryFSize = zipEntry.getSize(); if(zipEntry.isDirectory()){ logger.warn("The entry " + strEntryFName + " IS directory, to ignore it."); continue; } if(!(strEntryFName.startsWith(strClaimsFilePattern) || strEntryFName.trim().equals(strManifestFName))){ bEntryMismatched = true; strErrMsg = "Invalid Zip Entry Name " + strEntryFName + ", Decompressing to ErrorZone..."; logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); }else if(!mapFNameToSize.containsKey(strEntryFName) || !mapFNameToSize.get(strEntryFName).equals(Long.toString(lEntryFSize))){ bEntryMismatched = true; strErrMsg = "Entry-filename: " + strEntryFName + ", Entry-filesize: " + lEntryFSize + " mismatch with the value " + mapFNameToSize.get(strEntryFName) + " in Manifest file! Decompressing to ErrorZone..."; logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); } isEntryFile = zipFile.getInputStream(zipEntry); osEntryFile = bEntryMismatched ? new FileOutputStream(strErrorZone + File.separator + strEntryFName) : new FileOutputStream(strTargetFilePath + File.separator + strEntryFName); osEntryArchive = new FileOutputStream(strArchivePath + File.separator + strEntryFName); while(isEntryFile.read(bReadBuf) != -1){ osEntryFile.write(bReadBuf); osEntryArchive.write(bReadBuf); } isEntryFile.close(); osEntryFile.close(); osEntryArchive.close(); if(!bEntryMismatched){ iEntryCounter++; logger.info("Matched Entry No. " + iEntryCounter + " | Entry Name: " + strEntryFName +", Entry File Size: " + lEntryFSize); }else logger.info("The mismatched Entry file was Decompressed to ErrorZone and Archived!"); } logger.info("Zip No. " + iZip + " | Zip Filename: " + strZipFName + ", Entries contained: " + zipFile.size() + ", Entries decompressed: " + iEntryCounter); zipFile.close(); if(fZipArray[iZip].delete()) logger.info("Deleted the zip file!"); else logger.warn("Failed to delete the zip file!"); } logger.info("Finished process the zip file(s) in location " + strZipFilePath); }catch(IOException ioE){ strErrMsg = "IOException happned with msg: " + ioE.getMessage(); logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); if(fZipArray != null && fZipArray.length > 0){ for(int iZip = 0; iZip < fZipArray.length; iZip++){ if(!fZipArray[iZip].renameTo(new File(strErrorZone + File.separator + fZipArray[iZip].getName()))){ logger.warn("Failed to move the left zip file " + fZipArray[iZip].getName() + " to ErrorZone " + strErrorZone); }else logger.info("Moved the zip file " + fZipArray[iZip].getName() + " to ErrorZone " + strErrorZone); } } }catch(Exception exc){ strErrMsg = "Exception happened with msg: " + exc.getMessage(); logger.error("[" + PropSet.GIFInterfaceId + "] " + strErrMsg); } } class FileListFilter implements FilenameFilter { private String name; private String extension; public FileListFilter(String name, String extension) { this.name = name; this.extension = extension; } public boolean accept(File directory, String filename) { boolean fileOK = true; if (name != null) { fileOK &= filename.startsWith(name); } if (extension != null) { fileOK &= filename.endsWith('.' + extension); } return fileOK; } } class PropSet { static int FILE_WRITE_BUFSIZE = 1024; static String GIFInterfaceId = "I2001_GLB"; }
由于需求特定,代码实现有所局限。
看到Snowolf对带有压缩算法的压缩/解压缩的研究,在实际应用中需要面对不同的问题和场景,如文件读写同步/目录压缩及解压缩/中文编码等。故本文章也会适时更新。
发表评论
-
uniVocity-parsers:一款强大的CSV/TSV/定宽文本文件解析库(Java)
2015-04-27 00:21 5847uniVocity-parsers 是一个开源的Java项目。 ... -
GWT通信机制初探
2010-01-29 10:30 8610GWT RPC:GWT提供类似Swing的界面开发模式(基于U ... -
Java中禁止的包名(Prohibited package name)
2009-08-19 00:18 36326由于定义了以java开始的包(java.mypackage), ... -
Log4j for Tomcat5.5
2009-05-31 13:41 2341在Tomcat5.5之前,可以通过server.xml下< ... -
利用JDBC操作Oracle CLOB和BLOB类型数据
2009-05-11 09:41 10214对LOB数据(包括CLOB BLOB NCLOB BFILE类 ... -
解决eclipse3.4下错误:Exception starting filter struts2
2009-05-04 12:26 1648项目中用到struts2,跟以前用到过的struts有很大区别 ... -
JDBC高级编程:操作结果集
2009-04-23 10:26 4805自JDBC2.0之后,可滚动(Scrollabl ... -
Java动态绑定探讨
2009-04-20 09:26 1386本文转 “子 孑” 博客:http://zhangjunhd. ... -
区别类方法、实例方法和字段的调用
2009-04-16 15:41 2204本例根据一个实例来说明类方法和实例方法调用的区别,以 ... -
Tomcat类加载器体系结构
2009-04-03 22:09 5635与Java语言相似,Tomcat提供了分级类加载机制,当 ... -
多线程的弹球游戏实现
2009-04-02 00:16 2123大二开始学习Java的时候 ... -
Java类加载器
2009-03-31 21:06 4213Java是介于编译型和解释型之间的编程语言,编译器将jav ... -
JSON
2008-12-29 22:28 1678JSON建构于两种结构: ...
相关推荐
java压缩和解压缩
java 压缩和解压缩为zip文件 有测试类,可以直接测试。效果还不错,现网已经运行好长时间了
这几天做一个操作docx的文档的功能,Apache的POI功能有限,就参考了相关资料写的一个Java压缩和解压zip文件的工具类。全部使用的是Java自带的类库,中文的文件名会有乱码的问题。
结合以下两篇文章做了个压缩和解压缩工程,开放全部源码! <br>http://agilejava.blogbus.com/logs/2005/09/1406938.html<br>http://blog.csdn.net/robin622/archive/2008/01/01/2008203.aspx
NULL 博文链接:https://137459045.iteye.com/blog/1355221
java实现霍夫曼(huffman)树的压缩和解压缩,支持对文档的压缩和解压缩
主要介绍了实例展示使用Java压缩和解压缩7z文件的方法,用到了7-zip的开源项目7-zip-JBinding,需要的朋友可以参考下
本文主要介绍java压缩和解压缩Zip、Jar、Gzip文件的知识,这里整理了相关资料,并附示例代码有兴趣的小伙伴可以参考下
java中如何实现压缩和解压缩,zip,unzip,java中如何实现压缩和解压缩,zip,unzip
java端文件和文件夹的压缩和解压缩。经过测试,通过
本代码实例演示如何用java语言实现apk文件的压缩和解压缩。
本程序实现了利用 Huffman 编码对图像进行无损压缩和解压缩。Huffman 编码是一种基于字符出现频率构建相应前缀码的无损数据压缩算法。 使用方法: 1. 需要安装 OpenCV 和 Numpy 库: pip install opencv-python ...
使用java语言实现Huffman对任意文件做到压缩和解压缩,压缩后的文件只能通过解压缩来解压后才能读取
用java实现huffman编码,Encode.java实现文件的压缩,Decode.java 实现文件的解压缩
使用apache的第三方包,实现对文件的压缩和解压缩,解决jdk自带的实现中,中文文件名乱码问题,压缩和解压缩时,可以设置文件名的编码方式
压缩和解压缩源码
使用Java实现文件的压缩和解压缩
JAVA 压缩和解压缩,JAVA 压缩和解压缩
两种方法 对字符串进行压缩和解压缩,inflater和deflater。
Java标准库的java.util.zip包通常适用于简单的ZIP格式压缩与解压缩,Apache Commons Compress和Java Zip4j提供了更多的压缩格式和功能选项。对于大型数据集和高性能要求的场景,还可以考虑使用专门的压缩工具或算法...