2

I have a requirement where I have to create 1 xml file inside my .zip folder.

Can you please let me know how can I achieve this using java/scala?

Also let me know can I directly update .zip folder Array[Byte] and add extra Array[Byte] for my new xml file ?

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
Nishan
  • 375
  • 3
  • 16
  • Simply use the [zip `FileSystem`](https://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html) provided by Java NIO. – Boris the Spider Sep 23 '15 at 07:23

1 Answers1

2

If you are using Java 7 and later, then you should use Zip File System

The following code sample shows how to create a zip file system and copy a file to the new zip file system.

import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;

public class ZipFSPUser {
    public static void main(String [] args) throws Throwable {
        Map<String, String> env = new HashMap<>(); 
        env.put("create", "true");
        // locate file system by using the syntax 
        // defined in java.net.JarURLConnection
        URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");

       try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
            Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
            Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");          
            // copy a file into the zip file
            Files.copy( externalTxtFile,pathInZipfile, 
                    StandardCopyOption.REPLACE_EXISTING ); 
        } 
    }
}

This post could help you: Appending files to a zip file with Java

Community
  • 1
  • 1
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • 1
    Hi @Andriy Kuba thanks for your reply. can you please let me know istead of zip file URI, can I do the same thing with my .zip file Array[Byte] from database and insert another file Array[Byte]? – Nishan Apr 11 '16 at 12:05