luca
luca

Reputation: 3328

Unzip file with special character through java.util.zip library

I have my zip file with several files inside it. When I run my unzip code:

public ArrayList<String> unzip(String zipFilePath, String destDirectory, String filename) throws IOException {
        ArrayList<String> pathList = new ArrayList<String>();
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            //          Original file name  
            //          String filePath = destDirectory + File.separator + entry.getName();
            int _ordPosition = entry.getName().indexOf("_ord");
            if (_ordPosition<0)
                throw new DatOrderException("Files inside zip file are not in correct format (please order them with _ordXX string)");
            String ord = entry.getName().substring(_ordPosition,_ordPosition+6); 
            String filePath = destDirectory + File.separator + filename + ord + "."+ FilenameUtils.getExtension(entry.getName());
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                pathList.add(filePath);
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
        return pathList;
    }

if file inside archive contains special character like º I receive exception with Malformed message on row

ZipEntry entry = zipIn.getNextEntry();

Is it possible to rename this file or fix this error? Thanks

Upvotes: 0

Views: 1803

Answers (2)

luca
luca

Reputation: 3328

As @Andrew Kolpakov suggested, with

 ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath),Charset.forName("IBM437"));

it seems to work

Upvotes: 0

Andrew Kolpakov
Andrew Kolpakov

Reputation: 439

Try to read zip file with correct characters encoding - use ZipInputStream(java.io.InputStream, java.nio.charset.Charset) instead of ZipInputStream(java.io.InputStream)

Upvotes: 1

Related Questions