Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java(ZipEntry)-如果文件名包含捷克字符(č;ž;ř;等),则在读取下一个条目时失败_Java_Inputstream_Zipfile_Zipinputstream - Fatal编程技术网

Java(ZipEntry)-如果文件名包含捷克字符(č;ž;ř;等),则在读取下一个条目时失败

Java(ZipEntry)-如果文件名包含捷克字符(č;ž;ř;等),则在读取下一个条目时失败,java,inputstream,zipfile,zipinputstream,Java,Inputstream,Zipfile,Zipinputstream,我有一个问题,当我试图解压文件包含特殊字符的文件 假设我有一个带有图像文件的zip文件gallery.zip gallery.zip - file01.jpg - dařbuján.jpg 我的方法开始: public List<File> unzipToTemporaryFolder(ZipInputStream inputStream) throws IOException { List<File> files = new Linke

我有一个问题,当我试图解压文件包含特殊字符的文件

假设我有一个带有图像文件的zip文件gallery.zip

gallery.zip
  - file01.jpg
  - dařbuján.jpg
我的方法开始:

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream)
        throws IOException {
    List<File> files = new LinkedList<File>();
    ZipEntry entry = null;
    int count;
    byte[] buffer = new byte[BUFFER];

    while ((entry = inputStream.getNextEntry()) != null) {
public List unziptoTemporary文件夹(ZipInputStream-inputStream)
抛出IOException{
列表文件=新建LinkedList();
ZipEntry条目=null;
整数计数;
字节[]缓冲区=新字节[缓冲区];
while((entry=inputStream.getnextery())!=null){
当我试图读取文件dařbuján.jpg时,它在inputStream.getnextry()中失败,因为捷克字母“ř”和“á”。它可以与其他文件很好地配合使用,例如空格(104 25.jpg或简单的file.jpg等)。您能帮我吗?

使用指定的字符集创建ZipInputStream吗

 ZipInputStream(InputStream in, Charset charset)

使用使用指定的字符集创建ZipInputStream

 ZipInputStream(InputStream in, Charset charset)


好的,我用commons compress解决了这个问题。如果有人感兴趣,这里是我的方法:

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream,
        File tempFile) throws IOException {
    List<File> files = new LinkedList<File>();
    int count;
    byte[] buffer = new byte[BUFFER];

    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(tempFile, "UTF-8");
    Enumeration<?> entires = zf.getEntries();
    while(entires.hasMoreElements()) {
        org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry = (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement();
        if(entry.isDirectory()) {
            unzipDirectoryZipEntry(files, entry);
        } else {            
            InputStream zin = zf.getInputStream(entry);                 

            File temp = File.createTempFile(entry.getName().substring(0, entry.getName().length() - 4) + "-", "." + entry.getName().substring(entry.getName().length() - 3, entry.getName().length()));                                     

            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temp), BUFFER);
            while ((count = zin.read(buffer, 0, BUFFER)) != -1) {
                outputStream.write(buffer, 0, count);
            }
            outputStream.flush();
            zin.close();
            outputStream.close();
            files.add(temp);            
        }
    }
    zf.close();
    return files;
}
public List unziptoTemporary文件夹(ZipInputStream-inputStream,
文件tempFile)引发IOException{
列表文件=新建LinkedList();
整数计数;
字节[]缓冲区=新字节[缓冲区];
org.apache.commons.compress.archivers.zip.ZipFile zf=new org.apache.commons.compress.archivers.zip.ZipFile(tempFile,“UTF-8”);
枚举entires=zf.getEntries();
while(entires.hasMoreElements()){
org.apache.commons.compress.archivers.zip.ZipArchiveEntry条目=(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement();
if(entry.isDirectory()){
unzipDirectoryZipEntry(文件、条目);
}否则{
InputStream zin=zf.getInputStream(条目);
File temp=File.createTempFile(entry.getName().substring(0,entry.getName().length()-4)+“-”,“+”entry.getName().substring(entry.getName().length()-3,entry.getName().length());
OutputStream OutputStream=new BufferedOutputStream(new FileOutputStream(temp),BUFFER);
而((计数=zin.read(缓冲区,0,缓冲区))!=-1){
写入(缓冲区,0,计数);
}
outputStream.flush();
zin.close();
outputStream.close();
文件。添加(临时);
}
}
zf.close();
归还文件;
}

好的,我用commons compress解决了这个问题。如果有人感兴趣,这里是我的方法:

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream,
        File tempFile) throws IOException {
    List<File> files = new LinkedList<File>();
    int count;
    byte[] buffer = new byte[BUFFER];

    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(tempFile, "UTF-8");
    Enumeration<?> entires = zf.getEntries();
    while(entires.hasMoreElements()) {
        org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry = (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement();
        if(entry.isDirectory()) {
            unzipDirectoryZipEntry(files, entry);
        } else {            
            InputStream zin = zf.getInputStream(entry);                 

            File temp = File.createTempFile(entry.getName().substring(0, entry.getName().length() - 4) + "-", "." + entry.getName().substring(entry.getName().length() - 3, entry.getName().length()));                                     

            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temp), BUFFER);
            while ((count = zin.read(buffer, 0, BUFFER)) != -1) {
                outputStream.write(buffer, 0, count);
            }
            outputStream.flush();
            zin.close();
            outputStream.close();
            files.add(temp);            
        }
    }
    zf.close();
    return files;
}
public List unziptoTemporary文件夹(ZipInputStream-inputStream,
文件tempFile)引发IOException{
列表文件=新建LinkedList();
整数计数;
字节[]缓冲区=新字节[缓冲区];
org.apache.commons.compress.archivers.zip.ZipFile zf=new org.apache.commons.compress.archivers.zip.ZipFile(tempFile,“UTF-8”);
枚举entires=zf.getEntries();
while(entires.hasMoreElements()){
org.apache.commons.compress.archivers.zip.ZipArchiveEntry条目=(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement();
if(entry.isDirectory()){
unzipDirectoryZipEntry(文件、条目);
}否则{
InputStream zin=zf.getInputStream(条目);
File temp=File.createTempFile(entry.getName().substring(0,entry.getName().length()-4)+“-”,“+”entry.getName().substring(entry.getName().length()-3,entry.getName().length());
OutputStream OutputStream=new BufferedOutputStream(new FileOutputStream(temp),BUFFER);
而((计数=zin.read(缓冲区,0,缓冲区))!=-1){
写入(缓冲区,0,计数);
}
outputStream.flush();
zin.close();
outputStream.close();
文件。添加(临时);
}
}
zf.close();
归还文件;
}

但它失败了…具体情况如何?java.lang.IllegalArgumentException是的,同时我用谷歌搜索了你的问题。很容易找到。真的吗?我找不到任何有效的解决方案…那么我猜你不是在java 7上。对于早期版本,你运气不佳。你看过错误报告了吗?它失败了…具体情况如何?java.lang.IllegalArgumentException是的,同时我用谷歌搜索了你的问题。很容易找到。真的吗?我找不到任何有效的解决方案…那么我猜你不是在Java 7上。对于早期版本,你运气不好。你读过错误报告吗?有一个问题,ZipInputStream只接受一个参数…InputStream in。没有字符集。哦,我明白了。这是Java 7的东西。嗯有一个问题是ZipInputStream只接受一个参数…InputStream in。没有字符集。哦,我明白了。这就是Java 7。