Tag: zip

从ResourceStream中提取压缩文件会引发错误“无效的存储块长度”

我试图从我当前的JAR中提取ZIP文件: InputStream resource = getClass().getClassLoader().getResourceAsStream(name); 这会得到正确的InputStream ,但是当我尝试使用以下代码解压缩它时会出错(我将每个文件存储到Hashmap ): public static HashMap readZip(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; HashMap list = new HashMap(); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry entry = zipInputStream.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { StringBuilder stringBuilder = new StringBuilder(); while (IOUtils.read(zipInputStream, buffer) > 0) { stringBuilder.append(new […]

给定特定路径的jar文件的内容

我有一个名为“san.jar”的jar文件,其中包含各种文件夹,如“类”,“资源”等。比如说,我有一个文件夹结构,如“资源/资产/图像”,其中有各种图像,我没有关于它们的任何信息,如图像名称或文件夹下的图像数量,因为jar文件是私有的,我不允许解压缩jar。 目标:我需要获取给定路径下的所有文件而不迭代整个jar文件。 现在我正在做的是遍历每个条目,每当我遇到.jpg文件时,我都会执行一些操作。 这里只读“资源/资产/图像”,我正在遍历整个jar文件。 JarFile jarFile = new JarFile(“san.jar”); for(Enumeration em = jarFile.entries(); em.hasMoreElements();) { String s= em.nextElement().toString(); if(s.contains(“jpg”)){ //do something } } 现在我正在做的是遍历每个条目,每当我遇到.jpg文件时,我都会执行一些操作。 这里只读“资源/资产/图像”,我正在遍历整个jar文件。

无法使用java制作文件夹来压缩文件?

这里我有Books文件夹里面的文件夹(Books)结构我有文件夹叫做物理,化学,科学,英语。我传给Books文件夹作为zipDeleteFile但是里面的所有文件夹都必须转换成物理文件夹(Books)和physics.zip ,chemistry.zip,science.zip,english.zip。但这段代码不起作用。 “ public void foldertToZip(File zipDeleteFile) { //System.out.println(zipDeleteFile); File directoryToZip = zipDeleteFile; List fileList = new ArrayList(); //System.out.println(“—Getting references to all files in: ” + directoryToZip.getCanonicalPath()); getAllFiles(directoryToZip, fileList); //System.out.println(“—Creating zip file”); writeZipFile(directoryToZip, fileList); //System.out.println(“—Done”); } public static void getAllFiles(File dir, List fileList) { try { File[] files = dir.listFiles(); for (File file : files) […]

将ZipEntry复制到新ZipFile的惯用方法是什么?

我正在编写一个工具来在DOCX文件中进行一些小文本替换,这是一种压缩格式。 我的方法是使用ZipOutputStream将ZipEntry内容从原始文件中的条目复制到修改后的文件中。 对于大多数DOCX文件,这种方法很有效,但偶尔我会遇到ZipException有关我写的内容与ZipEntry包含的元信息之间的差异(通常是压缩大小的差异)。 这是我用来复制内容的代码。 为简洁起见,我删除了error handling和文档处理; 到目前为止,我还没有遇到过文档条目的问题。 ZipFile original = new ZipFile(INPUT_FILENAME); ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(OUTPUT_FILE)); Enumeration entries = original.entries(); byte[] buffer = new byte[512]; while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); if (“word/document.xml”.equalsIgnoreCase(entry.getName())) { //perform special processing } else{ outputStream.putNextEntry(entry); InputStream in = original.getInputStream(entry); while (0 < in.available()){ int read = in.read(buffer); […]

将动态内容压缩为ServletOutputStream

我想压缩动态创建的内容并直接写入ServletOutputStream,而不是在压缩之前将其保存为服务器上的文件。 例如,我创建了一个Excel工作簿和一个StringBuffer,其中包含带有SQL模板的字符串。 在压缩文件并写入ServletOutputStream进行下载之前,我不想将动态内容保存到服务器上的.xlsx和.sql文件中。 示例代码: ServletOutputStream out = response.getOutputStream(); workbook.write(byteArrayOutputStream); zipIt(byteArrayOutputStream,out); public static boolean zipIt(ByteArrayOutputStream input, ServletOutputStream output) { try { ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); ZipEntry zipEntry = new ZipEntry(“test.xlsx”); zos.putNextEntry(zipEntry); if (input != null) { zipEntry.setSize(input.size()); zos.write(input.toByteArray()); zos.closeEntry(); } } catch (IOException e) { logger.error(“error {}”, e); return false; } return true; }

如何在Java中将zip文件移动到blob列?

我有一个Java程序,它创建了许多xml文件,然后将它们压缩并将它们保存到文件系统中。 稍后在程序中我想将相同的zip文件放入我的oracle数据库的blob列中。 问题是我不知道该怎么做。 我不需要阅读它或对数据做任何事情,只需将其移动到数据库以进行持久的中央存储。 谢谢!

java:更改ZipEntry名称

我有以下代码将文本文件写入zip: FileOutputStream fOut = new FileOutputStream(fullFilename, false); BufferedOutputStream bOut = new BufferedOutputStream(fOut); ZipOutputStream zOut = new ZipOutputStream(bOut); zOut.putNextEntry(new ZipEntry(“aFile1.txt”)); //Do some processing and write to zOut… zOut.write(…); (…) zOut.closeEntry(); zOut.close(); //Etc (close all resources) 我需要在写入后更改zipEntry的文件名(因为它的名称将取决于其编写的内容)。 此外,只有在最终文件名已知时才能写入缓冲区并写入文件(因为文件大小可能非常大:内存不足)。 任何有关如何做到这一点的建议将不胜感激! 谢谢,托马斯

是否有一种独立于平台的方式以编程方式从zip / jar中删除文件?

希望无需解压缩和重新打包存档。

Zip文件在上传到服务器时损坏

我的java程序将zip文件从我的系统上传到FTP服务器。 uploadfile()是一个包含上传代码的函数。 uploadfile( “192.168.0.210”, “muruganp”, “vm4snk”, “/首页/管理/ GATE521 / LN_RB_Semivalid2junk /输出/” +日期+ “_ RB1.zip”, “/文件服务器/ filesbackup / EMAC /” +日期+“_ RB1 。压缩”); public static boolean uploadfile(String server, String username, String Password, String source_file_path, String dest_dir) { FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(server); ftp.login(username, Password); System.out.println(“Connected to ” + server + “.”); […]

压缩hadoop mapreduce输出的最简单有效的方法

我可以用map压缩mapreduce输出到gzip “mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec” 是否可以直接为hadoop实现zip编解码器? Zip是容器,但每个存档只需要一个文件,那么使用CompressionCodec接口创建ZipCodec会很容易吗? 或者,也许有一种将gz文件转换为zip的有效方法,因为它们可以使用相同的deflate算法?