Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When UnzipUtil.createZipInputStream(ZipModel, FileHeader, char[]) throws an Exception it doesn't close InputStreams #58

Closed
daelfdude opened this issue Sep 3, 2019 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@daelfdude
Copy link

daelfdude commented Sep 3, 2019

I have a unit test asserting that a file is password protected. As an assertion on that, I check that opening the zip file with a null password fails. However when I do that, the file being asserted upon becomes undeletable due to the unclosed input streams this method creates.

@srikanth-lingala srikanth-lingala self-assigned this Sep 5, 2019
@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Sep 5, 2019

I used the following code to test, and this code deletes the zip file. I tested it on a mac though. Which os are you using? Does your tests (or user running the tests) have the rights to delete a file?

ZipFile zipFile = new ZipFile("/Users/srikanth/Downloads/test.zip", "password".toCharArray());
    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(EncryptionMethod.AES);
    zipFile.addFile(TestUtils.getTestFileFromResources("sample.pdf"), zipParameters);

    try {
      zipFile = new ZipFile("/Users/srikanth/Downloads/test.zip");
      zipFile.extractAll("/Users/srikanth/Downloads/");
    } catch (Exception e) {
      assertThat(zipFile.getFile().delete()).isTrue();
      throw e;
    }

@daelfdude
Copy link
Author

daelfdude commented Sep 6, 2019 via email

@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Sep 8, 2019

I was able to reproduce on Windows with the below code:

@Test
  public void zipTest() throws IOException {

    ZipFile zipFile = new ZipFile("c:\\test.zip", "password".toCharArray());
    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(EncryptionMethod.AES);
    zipFile.addFile(TestUtils.getTestFileFromResources("sample.pdf"), zipParameters);

    try {
      zipFile = new ZipFile("c:\\test.zip"); // intentionally not providing password to generate an exception
      try(InputStream inputStream = zipFile.getInputStream(zipFile.getFileHeaders().get(0))) {
        inputStream.read(new byte[4096]);
      }
      fail("Should throw an exception");
    } catch (Exception e) {
      assertThat(zipFile.getFile().delete()).isTrue();
      throw e;
    }
  }

I will include a fix for it.

@srikanth-lingala
Copy link
Owner

Fixed in v2.1.3 released today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants