java解析zip中文乱码问题解决

    private List<ExcelModel> readZip(MultipartFile file) {
    
    
        // TODO 这里使用GBK方式不要使用UTF8
        try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(file.getInputStream(), "GBK")) {
    
    
            List<ExcelModel> importExcel = new ArrayList<>();
            ArchiveEntry zipEntry;
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
    
    
                String fileName = zipEntry.getName().toLowerCase(Locale.ROOT);
                String fileNameTemp = fileName.toLowerCase(Locale.ROOT);   
                String fillName = fileName.substring(0, fileName.lastIndexOf("."));
                String[] split = fillName.split("/");
                String documentName = split[split.length - 1];
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = zipInputStream.read(buffer)) != -1) {
    
    
                    outputStream.write(buffer, 0, bytesRead);
                }
                InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
                //TODO 处理文件
                }
            }
            zipInputStream.close();
            return importExcel;
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/Ellis_li/article/details/131222835