Last active
August 31, 2022 17:53
-
-
Save anna-dolbina/28019cfd656bd52a89c43b1c0e3632a1 to your computer and use it in GitHub Desktop.
This sample demonstrates how to add a new file (or overwrite the existing one) in a ZIP archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.Enumeration; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import java.util.zip.ZipOutputStream; | |
public class ZipUtility { | |
private final File zipFilePath; | |
public ZipUtility(File zipFilePath) throws IOException { | |
if (zipFilePath == null) { | |
throw new IllegalArgumentException("The source path cannot be null"); | |
} | |
if(!zipFilePath.isFile()) { | |
throw new IllegalArgumentException("The passed path does not denote a valid file"); | |
} | |
this.zipFilePath = zipFilePath; | |
} | |
public void addOrReplaceEntry(String entry, InputStream entryData) throws IOException { | |
File temporaryFile = File.createTempFile(zipFilePath.getName(), null); | |
temporaryFile.delete(); | |
zipFilePath.renameTo(temporaryFile); | |
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath)); | |
addOrReplaceEntry(temporaryFile, entry, entryData, zipOutputStream); | |
zipOutputStream.close(); | |
temporaryFile.delete(); | |
} | |
public static void addOrReplaceEntry(File source, String entry, InputStream entryData, ZipOutputStream zipOutputStream) throws IOException { | |
ZipFile zipSourceFile = new ZipFile(source); | |
writeZipEntry(zipOutputStream, entry, entryData); | |
Enumeration<? extends ZipEntry> zipEntries = zipSourceFile.entries(); | |
while (zipEntries.hasMoreElements()) { | |
ZipEntry zipEntry = zipEntries.nextElement(); | |
String entryName = zipEntry.getName(); | |
if (!entry.equalsIgnoreCase(entryName)) { | |
try { | |
writeZipEntry(zipOutputStream, entryName, zipSourceFile.getInputStream(zipEntry)); | |
} catch (Exception e) { | |
logException(e); | |
} | |
} | |
} | |
zipSourceFile.close(); | |
} | |
private static void writeZipEntry(ZipOutputStream zipOutputStream, String entryName, InputStream entryData) throws IOException { | |
ZipEntry entry = new ZipEntry(entryName); | |
byte[] buf = new byte[1024]; | |
zipOutputStream.putNextEntry(entry); | |
int len; | |
while ((len = entryData.read(buf)) > 0) { | |
zipOutputStream.write(buf, 0, len); | |
} | |
zipOutputStream.closeEntry(); | |
} | |
private static void logException(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, this will be very slow on large archives as you are inflating/deflating each entry each time. You could do this efficiently with Apache Commons Compress's ZipArchiveInput/OutputStream's see here or using the newer FileSustems API