Java : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream

Viewed 2151

I woudlike to write some data on specific cell of my excel file but I have always the same error. I use Apache POI to write and read into the template file :

Exception in thread "Thread-4" org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream (org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream is in unnamed module of loader 'app'; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader 'bootstrap') 
  

Main :

private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private final File pathTemplate = newFile(Objects.requireNonNull(classLoader.getResource("excel/template.xlsx")).toURI());
        
        
         public void updateRapport(int indexSheet, int rowwnum, int cellnum, String value, File file) throws IOException, InvalidFormatException {
        
                Workbook workbook = WorkbookFactory.create(new File(file.getPath()));
                // Get Sheet
                Sheet sheet = workbook.getSheetAt(indexSheet);
        
                System.out.println(sheet.getSheetName());
        
                // Get Row
                Row row = sheet.getRow(rowwnum);
        
                // Get the Cell
                Cell cell = row.getCell(cellnum);
        
                // Update the cell
                cell.setCellType(CellType.STRING);
                cell.setCellValue(value);
        
                // Write the output to the file
                try(FileOutputStream fileOut = new FileOutputStream(file.getName()))
                {
                    workbook.write(fileOut);
                }
        
                // Closing the workbook
                workbook.close();
            }
    
    
    public static void main(String[] args) {
            updateRapport(0,1,2,"ok",pathTemplate);
        }
2 Answers

It is because when your resource in in jar-file you can not treat it as file.
If you need a file, write the resource content into a temporary file then use it.
Something like this:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.junit.Test;

public class FirstTest {
    @Test
    public void resourceTest() throws IOException {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final InputStream resource = classLoader.getResourceAsStream("resource");
        final File file = new File("d:/temp", "fileName");
        Files.copy(resource, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
}

This is how I create and delete the temp file

        File original, temp = null;
        original = new File(filename);
        try {
            temp = File.createTempFile("XlsB", ".xlsb", new File(PATH));
            FileUtils.copyFile(original, temp, true);
            ....
            ....
        } catch (IOException ex) {
            ...                
        }finally {                
            if (temp != null) {
                try {
                    FileUtils.delete(temp);
                } catch (IOException ex) {
                 ...
                }
            }
        }
Related