JavaFX – why do I get “Unexpected end of ZLIB input stream” when trying to open an unpacked .xlsx file with Apache POI? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JavaFX – why do I get “Unexpected end of ZLIB input stream” when trying to open an unpacked .xlsx file with Apache POI? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a raw folder in one of my packages under the src directory which contains .xlsx files. When I launch my program I initialize the Lists folder in the root directory and unpack my .xlsx files there using the following code:

public static void unpack(String type, boolean replace){
    File f;
    ArrayList<String> list = null;
    switch (type){
        case "N5":
            if(Main.curLocale.getLanguage().equals("ru")||Main.curLocale.getLanguage().equals("uk")||Main.curLocale.getLanguage().equals("be")) {
                String l[] = {"n5_home_body_rus.xlsx", "n5_travelling_studying_formulas_rus.xlsx", "n5_time_numbers_rus.xlsx", "n5_adjectives_rus.xlsx", "n5_verbs_rus.xlsx", "n5_borrowings_rus.xlsx", "n5_pron_adv_interj_prep_cnt_rus.xlsx"};
                list = new ArrayList<>(Arrays.asList(l));
            }else {
                String l[] = {"n5_home_body_eng.xlsx", "n5_travelling_studying_formulas_eng.xlsx", "n5_time_numbers_eng.xlsx", "n5_adjectives_eng.xlsx", "n5_verbs_eng.xlsx", "n5_borrowings_eng.xlsx", "n5_pron_adv_interj_prep_cnt_eng.xlsx"};
                list = new ArrayList<>(Arrays.asList(l));
            }
            break;
        case "N4":
            if(Main.curLocale.getLanguage().equals("ru")||Main.curLocale.getLanguage().equals("uk")||Main.curLocale.getLanguage().equals("be")) {
                String l[] = {"n4_nouns_1_rus.xlsx", "n4_nouns_2_rus.xlsx", "n4_verbs_rus.xlsx", "n4_adjectives_adverbs_misc_rus.xlsx", "n4_borrowings_rus.xlsx"};
                list = new ArrayList<>(Arrays.asList(l));
            }else {
                String l[] = {"n4_nouns_1_eng.xlsx", "n4_nouns_2_eng.xlsx", "n4_verbs_eng.xlsx", "n4_adjectives_adverbs_misc_eng.xlsx", "n4_borrowings_eng.xlsx"};
                list = new ArrayList<>(Arrays.asList(l));
            }
            break;
    }
    Optional<ButtonType> result;
    Alert a = new Alert(Alert.AlertType.CONFIRMATION);
    ButtonType yes = new ButtonType(Main.curBundle.getString("yes"));
    ButtonType no = new ButtonType(Main.curBundle.getString("no"));
    a.getButtonTypes().remove(0,2);
    a.getButtonTypes().addAll(yes,no);
    for(String lst : list) {
        result = null;
        f = new File("Lists/" + lst);
        if (f.exists() && !replace) {
            a.setHeaderText(lst + Main.curBundle.getString("already_exists"));
            result = a.showAndWait();
        }
        if (!f.exists() || (result != null && result.get() == yes)) {
            try {
                InputStream is = Main.class.getResourceAsStream("raw/"+lst);
                byte buffer[] = new byte[is.available()];
                is.read(buffer);
                is.close();
                FileOutputStream fos = new FileOutputStream("Lists/"+lst);
                fos.write(buffer);
                fos.close();
                if(Main.dbc!=null){
                    if(!Main.listsFiles.containsKey(lst)) {
                        mDbxFile dbf = mDbxFile.upload(Main.dbc, "Lists\" + lst, "/Lists/" + lst, "/Lists");
                        if (dbf != null) {
                            Main.listsFiles.put(lst, dbf);
                        }
                    }
                }
            } catch (Exception exc) {
            }
        }
        if(Main.dbc!=null) {
            Main.saveOldDbxFiles(Main.listsFiles, new File(Main.LMETADATA));
        }
    }
}

Then I read the files using Apache POI in the following way:

File f = new File(Settings.CurList);
InputStream fis = null;
if(f.exists()&&f.isFile()) {
     fis = new FileInputStream(f);
}else{
     return;
}
XSSFWorkBook workbook = new XSSFWorkbook(fis);

This approach works when I launch from the IDE (I use Intellij Idea). Unfortunately, if I unpack the .xlsx files from a deployed application, the line XSSFWorkBook workbook = new XSSFWorkbook(fis); generates the following exception:

java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:168)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:130)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:57)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:91)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:273)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273)
at JlexMain.Main.getTableValues(Main.java:1390)
at JlexMain.Main.createTable(Main.java:1541)
at JlexMain.Main.showEditorScene(Main.java:713)
at JlexMain.Main$5$1.run(Main.java:315)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/32681650.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/14102484.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/8539234.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

I checked the files opening them in Excel. 4 of 7 are really corrupt: “n5_home_body_rus.xlsx”, “n5_travelling_studying_formulas_rus.xlsx”, “n5_time_numbers_rus.xlsx”, “n5_adjectives_rus.xlsx”. And there is a pattern: they are first 4 files in the array.

So, how can I bypass this strange behavior?

Answer

Using BufferedInputStream solved my problem:

InputStream is = Main.class.getResourceAsStream("raw/"+lst);
BufferedInputStream bs = new BufferedInputStream(is);
byte buffer[] = new byte[is.available()];
bs.read(buffer);
is.close();
bs.close();
FileOutputStream fos = new FileOutputStream("Lists/"+lst);
fos.write(buffer);
fos.close();

I still do not understand what was wrong with using a simple InputStream though.

We are here to answer your question about JavaFX – why do I get “Unexpected end of ZLIB input stream” when trying to open an unpacked .xlsx file with Apache POI? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji