Updating a file in a zip archive - java

I want to update a file inside a zip archive without the need to create a new archive. I am aware that it could be done by creating a new archive, coping over all other files and add the changed one in.
Here is the code to demonstrate the issue. The first section creates the initial zip archive containing one file with the content Hello world!. The second section should replace that content with Bye, bye!. The last section is unpacking the zip again so that the content can be expected:
try {
// Create the initial zip archive with the original file content
File file1 = new File(ReplaceFileInZipDemo.class.getResource("/helloworld.txt").toURI());
File zipFile = new File("output.zip");
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fos);
FileInputStream fis = new FileInputStream(file1);
ZipEntry zipEntry = new ZipEntry(file1.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
zipOut.close();
fos.close();
// Replace the original file content with a different file content, updating the file.
File file2 = new File(ReplaceFileInZipDemo.class.getResource("/goodbyeworld.txt").toURI());
Path zipfile = zipFile.toPath();
FileSystem fs = FileSystems.newFileSystem(zipfile, null);
Path pathInZipfile = fs.getPath(file1.getName());
Files.copy(file2.toPath() , pathInZipfile, StandardCopyOption.REPLACE_EXISTING );
// Extract the content of the updated file
String destinationDir = System.getProperty("java.io.tmpdir");
File targetDir = new File(destinationDir);
ZipInputStream i = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = null;
System.out.println("Original file content:");
Files.readAllLines(file1.toPath(), StandardCharsets.UTF_8).forEach(line -> System.out.println(line));
System.out.println("Expected replaced file content");
Files.readAllLines(file2.toPath(), StandardCharsets.UTF_8).forEach(line -> System.out.println(line));
while ((entry = i.getNextEntry()) != null) {
File destFile = new File(targetDir, entry.getName());
String name = destFile.getAbsolutePath();
File f = new File(name);
try (OutputStream o = Files.newOutputStream(f.toPath())) {
IOUtils.copy(i, o);
}
System.out.println("Content of extracted file " + name);
Files.readAllLines(f.toPath(), StandardCharsets.UTF_8).forEach(line -> System.out.println(line));
}
} catch (Exception e) {
e.printStackTrace();
}
The output that I get is this:
Original file content:
Hello world!
Expected replaced file content
Bye, bye!
Content of extracted file /tmp/helloworld.txt
Hello world!
The only reason that I could imagine this is not working as expected is because the content of the replacement comes from a file with a different name than in the archive. But when adding Files.delete(pathInZipfile); to remove the original file completely it is still there.
How can I replace the content of a file in an archive with the content of another file?

Related

zipping files in java memory produces a zip file but the files inside are empty

I am trying to zip multiple files in Java to be used in my jar. 2 files are images and 1 is an HTML temp file. Upon zipping these files, when I try to see the contents of the zip file, all 3 files have become empty. There is no error being thrown as the files are in the zip but they are empty for some reason. I need to save my zip file in memory.
Here is my zip code.
public static File zipPdf(File data, File cover) throws IOException {
ArrayList<ByteArrayOutputStream> zips = new ArrayList<>();
ClassLoader loader = RunningPDF.class.getClassLoader();
File image = new File(Objects.requireNonNull(loader.getResource("chekklogo.png")).getFile());
File man = new File(Objects.requireNonNull(loader.getResource("manlogo.jpg")).getFile());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry entry = new ZipEntry(data.getName());
zos.putNextEntry(entry);
ZipEntry entry2 = new ZipEntry(image.getName());
zos.putNextEntry(entry2);
ZipEntry entry3 = new ZipEntry(man.getName());
zos.putNextEntry(entry3);
} catch(IOException ioe) {
ioe.printStackTrace();
}
You forget to write the bytes. The putNextEntry just adds an entry. You need to explicitly write the bytes. Go with the following
File file = new File(filePath);
String zipFileName = file.getName().concat(".zip");
FileOutputStream fos = new FileOutputStream(zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry(file.getName()));
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
zos.write(bytes, 0, bytes.length);
zos.closeEntry();
zos.close();

How to unzip file with sub-directories in android?

When i was unzip file.it's make file name like Movies\Hollywood\spider-Man.
Actually Movies is a Folder,Hollywood is a Folder in Movies and spider-Man is file in Hollywood.
If Movies\Hollywood\spider-Man is a file while creating the zip it should be extracted as file, no matters whether it has extension or not (like *.mp4, *.flv)
You can rely on java APIs under namespace java.util.zip, the documentation link is here
Written some code which extracts only zip files, it should extract the file entry as file (no gzip, rar is supported).
private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException
{
int BUFFER = 8192;
File file = zipFile;
//This can throw ZipException if file is not valid zip archive
ZipFile zip = new ZipFile(file);
String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());
//Create destination directory
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
//Iterate overall zip file entries
while (zipFileEntries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile();
//If entry is directory create sub directory on file system
destinationParent.mkdirs();
if (!entry.isDirectory())
{
//Copy over data into destination file
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
//orthodox way of copying file data using streams
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
return true;//some error codes etc.
}
The routine does not perform any exception handling, please catch the ZipException and IOException inside driver code.

How to access a zip file into a zip file in Java

I'm trying to read .srt files that are located in zip file itself located in a zip file. I succeed to read .srt files that were in a simple zip with the extract of code below :
for (Enumeration enume = fis.entries(); enume.hasMoreElements();) {
ZipEntry entry = (ZipEntry) enume.nextElement();
fileName = entry.toString().substring(0,entry.toString().length()-4);
try {
InputStream in = fis.getInputStream(entry);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String ext = entry.toString().substring(entry.toString().length()-4, entry.toString().length());
But now i don't know how i could get to the zip file inside the zip file.
I tried using ZipFile fis = new ZipFile(filePath) with filePath being the path of the zip file + the name of zip file inside. It didn't recognize the path so i don't know if i am clear.
Thanks.
ZipFile only works with real files, because it's intended for use as a random access mechanism which needs to be able to seek directly to specific locations in the file to read entries by name. But as VGR suggests in the comments, while you can't get random access to the zip-inside-a-zip you can use ZipInputStream, which provides strictly sequential access to the entries and works with any InputStream of zip-format data.
However, ZipInputStream has a slightly odd usage pattern compared to other streams - calling getNextEntry reads the entry metadata and positions the stream to read that entry's data, you read from the ZipInputStream until it reports EOF, then you (optionally) call closeEntry() before moving on to the next entry in the stream.
The critical point is that you must not close() the ZipInputStream until you have finished reading the final entry, so depending what you want to do with the entry data you might need to use something like the commons-io CloseShieldInputStream to guard against the stream getting closed prematurely.
try(ZipInputStream outerZip = new ZipInputStream(fis)) {
ZipEntry outerEntry = null;
while((outerEntry = outerZip.getNextEntry()) != null) {
if(outerEntry.getName().endsWith(".zip")) {
try(ZipInputStream innerZip = new ZipInputStream(
new CloseShieldInputStream(outerZip))) {
ZipEntry innerEntry = null;
while((innerEntry = innerZip.getNextEntry()) != null) {
if(innerEntry.getName().endsWith(".srt")) {
// read the data from the innerZip stream
}
}
}
}
}
}
Find the code to extract .zip files recursively:
public void extractFolder(String zipFile) throws ZipException, IOException {
System.out.println(zipFile);
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.length() - 4);
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
if (currentEntry.endsWith(".zip"))
{
// found a zip file, try to open
extractFolder(destFile.getAbsolutePath());
}
}
}

Error in Selecting multiple files into one zip and downloading the zip file?

<% // Set the content type based to zip
response.setContentType("Content-type:text/zip");
response.setHeader("Content-Disposition", "attachment; filename=mytest.zip");
// List of files to be downloaded
List files = new ArrayList();
files.add(new File("C:/first.txt"));
files.add(new File("C:/second.txt"));
files.add(new File("C:/third.txt"));
ServletOutputStream out1 = response.getOutputStream();
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out1));
for (Object file : files)
{
//System.out.println("Adding file " + file.getName());
System.out.println("Adding file " + file.getClass().getName());
//zos.putNextEntry(new ZipEntry(file.getName()));
zos.putNextEntry(new ZipEntry(file.getClass().getName()));
// Get the file
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (Exception E) {
// If the file does not exists, write an error entry instead of file contents
//zos.write(("ERROR: Could not find file " + file.getName()).getBytes());
zos.write(("ERROR: Could not find file" +file.getClass().getName()).getBytes());
zos.closeEntry();
//System.out.println("Could not find file "+ file.getAbsolutePath());
continue;
}
BufferedInputStream fif = new BufferedInputStream(fis);
// Write the contents of the file
int data = 0;
while ((data = fif.read()) != -1) {
zos.write(data);
}
fif.close();
zos.closeEntry();
//System.out.println("Finished adding file " + file.getName());
System.out.println("Finished adding file " + file.getClass().getName());
}
zos.close();
%>
this is my actualy program , want to zip multiple file and then downloading it , is wat i am doing the way is right or wrong , am new to JAVA programming , can you help me out ???
Your for loop should look like this:
for (File file : files) {
...
or
for (String file : files) {
...
The way you declared file variable, makes compiler assume it's an Object, and not a File instance. Thus, you get compilation error, because there is no FileInputStream constructor accepting an Object. The file must either be a File or a String containing absolute path to a file.
Another error is the way, you're passing file's name to the ZipEntry.
Using:
file.getClass().getName()
will result in "java.io.File" or "java.lang.String", and not the file name.
To set the proper name of the file, use File#getName().

File Path appearing in the Zipped File

I have a java program as below for zipping a folder as a whole.
public static void zipDir(String dir2zip, ZipOutputStream zos)
{
try
{
File zipDir= new File(dir2zip);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
for(int i=0; i<dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if(f.isDirectory())
{
String filePath = f.getPath();
zipDir(filePath, zos);
continue;
}
FileInputStream fis = new FileInputStream(f);
ZipEntry anEntry = new ZipEntry(f.getPath());
zos.putNextEntry(anEntry);
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(){
String date=new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.Date());
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("Output/" + date + "_RB" + ".zip"));
zipDir("Output/" + date + "_RB", zos);
zos.close();
}
My query here is. The target folder(+date+_RB) to be zipped is present inside the folder named Output. After successful zipping, when I extract the zipped file, I find a folder Output inside which the (+date+_RB) required folder is present. I need not want that Output folder after the extraction of the zipped file, rather it should directly extract the required folder alone. Please advise on the same.
UPDATE:
I tried Isaac's answer. While extracting the resultant zip file, no folders are getting extracted. Only the files inside all the folders are getting extracted. I just dont need the folder "Output" alone in the resultant zip file. But what the program does is, it doesnt extracts all other folders inside the Output folder, rather it just extracts the files inside those folders. Kindly advise on how to proceed...
It happens because of this:
ZipEntry anEntry = new ZipEntry(f.getPath());
f.getPath() will return Output/ at the beginning of the string. This is due to the flow of your program and how it (mis)uses File objects.
I suggest you construct a File object called, say, tmp:
File tmp = new File(dirList[i]);
The change the construction of f:
File f = new File(zipDir, tmp.getPath());
Then, change this:
ZipEntry anEntry = new ZipEntry(f.getPath());
To this:
ZipEntry anEntry = new ZipEntry(tmp.getPath());
I didn't have time to actually test it, but in a nutshell, your problem is due to how the File object is constructed.

Categories