2

I uploaded my zip archive to the server and want to open .txt and .jpg files in it. I successfully get my archive in my Controller and get the name of each file via ZipEntry. Now I want to open it but for this I should get a full path to my file.

I haven't found how I can do that. Could you suggest some approach how to do that ?

Update

I try to use example have been suggested below but I am not be able open the file

ZipFile zFile = new ZipFile("trainingDefaultApp.zip");

I have got the FileNotFoundException

So I return to my start point. I have upload form in Java Spring application. In controller I had got a zip archive as byte[]

@RequestMapping(method = RequestMethod.POST)
public String create(UploadItem uploadItem, BindingResult bindingResult){
    try {
        byte[] zip = uploadItem.getFileData().getBytes();
        saveFile(zip);

Then I had got each ZipEntry

    InputStream is = new ByteArrayInputStream(zip);
    ZipInputStream zis = new ZipInputStream(is);

    ZipEntry entry = null;
    while ((entry = zis.getNextEntry()) != null) {
        String entryName = entry.getName();
        if (entryName.equals("readme.txt")) {
            ZipFile zip = new ZipFile(entry.getName()); // here I had got an exception

According to docs I did all right but as for me it is strange to pass the file name only and suspect that you successfully will open the file

tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
Winte Winte
  • 753
  • 5
  • 11
  • 24
  • What do you mean? You said you open it successfully; what do you not have a "full path" to? – Dave Newton Jun 13 '12 at 13:45
  • I have opened CommonsMultipartFile that contains my zip archive as byte[]. I convert it to ZipInputStream and in loop have got a ZipEntry for each file in my archive. Now I want to read this .txt file and .jpg file but ZipEntry contains only names of this files. So I haven't a full path to this files and can't open it as new File("d:/some.txt") – Winte Winte Jun 13 '12 at 13:50

2 Answers2

1

zipFile.getInputStream(ZipEntry entry) will return you the inputstream for the specific entry.

Check out the javadocs for ZipFile.getInputStream() - http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipFile.html#getInputStream(java.util.zip.ZipEntry).

Update:

I misread your question. For using the ZipInputStream, there is sample code on Oracle's website (http://java.sun.com/developer/technicalArticles/Programming/compression/) that shows you how to read from the stream. See the first code sample: Code

  • Sample 1: UnZip.java.

Copying here, it is reading from the entry and writing it directly to a file, but you could replace that with whatever logic you need:

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
   System.out.println("Extracting: " +entry);
   int count;
   byte data[] = new byte[BUFFER];
   // write the files to the disk
   FileOutputStream fos = new FileOutputStream(entry.getName());
   dest = new 
   BufferedOutputStream(fos, BUFFER);

   while ((count = zis.read(data, 0, BUFFER)) != -1) {
        dest.write(data, 0, count);
   }
}
tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
Paul Cichonski
  • 388
  • 1
  • 6
  • My bad, just saw that you have a ZipInputStream, not a ZipFile. There is sample code on Oracle's website (http://java.sun.com/developer/technicalArticles/Programming/compression/) that shows you how to read from the stream. See the first code sample: Code Sample 1: UnZip.java. – Paul Cichonski Jun 13 '12 at 14:12
  • Mmm..how can I use ZipFile? In consructor I should pass the File that I can't create without a full path to this file. In case when I pass the name of file as String I got FileNotFoundException – Winte Winte Jun 13 '12 at 14:27
  • I try this example. I have same excaeption. It says that file not found. For test I hardcoded the name of my archive "tetx.zip" but I have failed when try to create new FileInputStream("tetx.zip"). Is it wrong or the issue in another place? – Winte Winte Jun 13 '12 at 14:59
  • If you are able, you may want to post your code. I'm not sure what the issue is. – Paul Cichonski Jun 13 '12 at 15:17
  • Thanks for your help, Paul. I expend my question with code and my description. Probably it helps to unsderstand the cause of issue. – Winte Winte Jun 14 '12 at 07:14
  • link to sample code is broken. Possibly this is the one that was referenced, nice read! http://www.oracle.com/technetwork/articles/java/compress-1565076.html – Chacko Apr 20 '17 at 07:23
1

I resolve my uissue. The solution is work directly with ZipInputStream. Here the code:

    private void saveFile(byte[] zip, String name, String description) throws IOException {
    InputStream is = new ByteArrayInputStream(zip);
    ZipInputStream zis = new ZipInputStream(is);

    Application app = new Application();
    ZipEntry entry = null;
    while ((entry = zis.getNextEntry()) != null) {
        String entryName = entry.getName();
        if (entryName.equals("readme.txt")) { 
           new Scanner(zis); //!!!
           //... 
           zis.closeEntry();
Winte Winte
  • 753
  • 5
  • 11
  • 24