Tag: zip

通过Java复制Zip文件的最佳方法

经过一番研究: 如何创建Zip文件 和一些谷歌研究我想出了这个java函数: static void copyFile(File zipFile, File newFile) throws IOException { ZipFile zipSrc = new ZipFile(zipFile); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(newFile)); Enumeration srcEntries = zipSrc.entries(); while (srcEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) srcEntries.nextElement(); ZipEntry newEntry = new ZipEntry(entry.getName()); zos.putNextEntry(newEntry); BufferedInputStream bis = new BufferedInputStream(zipSrc .getInputStream(entry)); while (bis.available() > 0) { zos.write(bis.read()); } zos.closeEntry(); […]

Spring REST – 创建.zip文件并将其发送到客户端

我想创建包含我从后端收到的压缩文件的.zip文件,然后将此文件发送给用户。 2天我一直在寻找答案,找不到合适的解决方案,也许你可以帮助我:) 现在,代码是这样的:(我知道我不应该在spring控制器中完成所有操作,但不关心它,它只是为了测试目的,找到让它工作的方法) @RequestMapping(value = “/zip”) public byte[] zipFiles(HttpServletResponse response) throws IOException{ //setting headers response.setContentType(“application/zip”); response.setStatus(HttpServletResponse.SC_OK); response.addHeader(“Content-Disposition”, “attachment; filename=\”test.zip\””); //creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream); ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream); //simple file list, just for tests ArrayList files = […]

从InputStream解压缩文件并返回另一个InputStream

我正在尝试编写一个函数,它将接受带有压缩文件数据的InputStream ,并返回另一个带有解压缩数据的InputStream 。 压缩文件只包含一个文件,因此不需要创建目录等… 我试着看看ZipInputStream和其他人,但我对Java中的这么多不同类型的ZipInputStream到困惑。

用Java创建Zip文件夹中的文件夹

我的任务要求我将文件目录保存到zip文件夹中。 我唯一的问题是我需要将子文件夹保存为主目录中的文件夹。 文件系统看起来像 C\\Friends C:\\Friends\\Person1\\Information.txt C:\\Friends\\Person2\\Information.txt C:\\Friends\\Person3\\Information.txt 。 。 。 现在我只能在我的zip文件夹中写入txt文件,但在我的zip文件夹中,我需要保留该文件夹结构。 我知道我的代码现在的方式会告诉我我正在尝试写的文件是关闭的(无法访问)。 到目前为止我的职能: private String userDirectroy = “” //This is set earlier in the program public void exportFriends(String pathToFile) { String source = pathToFile + “.zip”; try { String sourceDir = userDirectory; String zipFile = source; try { FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout […]

将文件添加到ZIP文件

我试图将一些文件添加到ZIP文件,它创建文件但不添加任何内容。 代码1: String fulldate = year + “-” + month + “-” + day + “-” + min; File dateFolder = new File(“F:\\” + compname + “\\” + fulldate); dateFolder.mkdir(); String zipName = “F:\\” + compname + “\\” + fulldate + “\\” + fulldate + “.zip”; zipFolder(tobackup, zipName); 我的function: public static void zipFolder(File folder, String […]

如何检查生成的zip文件是否已损坏?

我们有一段代码在我们的系统上生成一个zip文件。 一切都很好,但有时这个由FilZip或WinZip打开的zip文件被认为已损坏。 所以这是我的问题:如果生成的zip文件已损坏,我们如何以编程方式检查? 以下是我们用于生成zip文件的代码: try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpFile)); byte[] buffer = new byte[16384]; int contador = -1; for (DigitalFile digitalFile : document.getDigitalFiles().getContent()) { ZipEntry entry = new ZipEntry(digitalFile.getName()); FileInputStream fis = new FileInputStream(digitalFile.getFile()); try { zos.putNextEntry(entry); while ((counter = fis.read(buffer)) != -1) { zos.write(buffer, 0, counter); } fis.close(); zos.closeEntry(); } catch […]

使用java.util.zip.ZipOutputStream时zip文件中的目录

假设我有一个文件t.txt,一个目录t和另一个文件t / t2.txt。 如果我使用linux zip实用程序“zip -r t.zip t.txt t”,我会得到一个zip文件,其中包含以下条目(unzip -l t.zip): Archive: t.zip Length Date Time Name ——– —- —- —- 9 04-11-09 09:11 t.txt 0 04-11-09 09:12 t/ 15 04-11-09 09:12 t/t2.txt ——– ——- 24 3 files 如果我尝试使用java.util.zip.ZipOutputStream复制该行为并为该目录创建zip条目,则java会抛出exception。 它只能处理文件。 我可以在zip文件中创建/ t2.txt条目并添加使用t2.txt文件内容但我无法创建目录。 这是为什么?

使用Java压缩文件:是否有限制?

我正在使用Java为我的应用程序创建一个备份例程。 但是,当zip文件超过4GB或文件(大约)超过65,000时,zip文件已损坏。 我也在测试Apache Commons Compression压缩到tar.gz,但文件名限制为100个字符。 我想测试这个压缩到压缩的API,但我想知道java zip的问题究竟是什么。 所以,真正的问题是:我做错了什么,它是Java Zip实现的限制,还是Zip格式本身的限制? 谢谢。

列出.zip目录而不提取

我正在使用Java构建文件资源管理器,我正在列出JTree中的文件/文件夹。 我现在要做的是当我到达一个压缩文件夹时,我想列出其内容,但不首先提取它。 如果有人有想法,请分享。

如何在Java中向现有zip文件添加条目?

可能重复: 使用Java将文件附加到zip文件 使用ZipOutputStream打开文件会覆盖它。 有没有办法保留文件,只是添加新条目?