0

I made controller for uploading zip files, and when I tested it through Postman, everything worked as intended.

Now I'm trying to create test for this controller, using custom test framework.

Part of my controller method for extracting zip:

 try (final ZipInputStream zis = new ZipInputStream(file.getInputStream())) {
            byte[] buffer = new byte[1024];
            ZipEntry entry;
// until here everything works, next line throws EOFException
            while ((entry = zis.getNextEntry()) != null) {
                String entryName = entry.getName();
                StringBuilder stringBuilder = new StringBuilder();

                int read;

                while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
                    stringBuilder.append(new String(buffer,0, read ));
                }

            }
        } catch (IOException e) {
            throw new GeneralServerException(e);
        }

Everything works fine up to line: ((entry = zis.getNextEntry()) != null):

Caused by: java.io.EOFException
    at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:405) ~[?:1.8.0_181]
    at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:321) ~[?:1.8.0_181]
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122) ~[?:1.8.0_181]
    ... 81 more

And this is part of my class for creating multipart request:

    private static final String FILE_PART_PATTERN = "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\";\r\nContent-Type: application/zip\r\n\r\n%s\r\n--";


        @Override
        public String getValue() {
            try {
                String fileContent = IOUtils.toString(getInputStream());
                String rawPart = String.format(FILE_PART_PATTERN, getName(), fileContent);
                return rawPart;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

And this is how I create file part of multipart request in test:

FilePart file = new FilePart("file", getClass().getResourceAsStream("/user_agreements.zip"));

I don't understand where the problem is and why this doesn't work. Method for extracting zips works fine when tested via Postman.

1 Answers1

0

I can only recommend checking a couple of things that were common traps I ran into when working with filessystems, as I see no explicit fault in your implementation right now:

  • Check if no file deletion occurred, especially between getting the stream and getNextEntry.
  • Check if you have the according access/permissions.
  • Check if the file is in use by something else, e.g. open somewhere or accessed otherwise (!)
  • Get the zip itself and see if it opens properly, maybe it was corrupted during data transfer

These are issues I have personally encountered at some point or another.

Koenigsberg
  • 1,726
  • 1
  • 10
  • 22
  • Thank you for your answer. I tried all of that, but it still doesn't work. While debugging, it doesn't recognize any ZipEntry, like there are no any, but end of file is set to false. I guess that may be the reason why exception is thrown. I still don't know how to solve this. –  Oct 16 '18 at 07:36
  • When I add to IOUtils.toString a charset cp437 then it recognize entries, but when it tries to read first entry, another exception is thrown: ZipException: invalid block type. –  Oct 16 '18 at 08:19
  • See if response #2 helps: https://stackoverflow.com/questions/7561031/zipinputstream-getnextentry-is-null-when-extracting-zip-files – Koenigsberg Oct 16 '18 at 08:33