【问题标题】:Extract a zip file denied access提取 zip 文件被拒绝访问
【发布时间】:2017-11-02 01:11:34
【问题描述】:

我正在尝试将 zip 文件解压缩到另一个位置,但我遇到了拒绝访问错误..

代码:

package org.spoutcraft.launcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZip
{
    List<String> fileList;
    private static final String INPUT_ZIP_FILE = "C:\\Level Up! Games" + File.separator + "Perfect World" + File.separator + "Updates" + File.separator + "Launcher.zip";
    private static final String OUTPUT_FOLDER = "C:\\Level Up! Games" + File.separator + "Perfect World";

    public static void main( String[] args )
    {
        UnZip unZip = new UnZip();
        unZip.unZipIt(INPUT_ZIP_FILE,OUTPUT_FOLDER);
    }

    /**
     * Unzip it
     * @param zipFile input zip file
     * @param output zip file output folder
     */
    public void unZipIt(String zipFile, String outputFolder)
    {
        byte[] buffer = new byte[1024];
        try
        {
            //create output directory is not exists
            File folder = new File(OUTPUT_FOLDER);
            if(!folder.exists())
            {
                folder.mkdir();
            }

            //get the zip file content
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while(ze!=null)
            {
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);

                System.out.println("file unzip : "+ newFile.getAbsoluteFile());

                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);             

                int len;
                while ((len = zis.read(buffer)) > 0) 
                {
                    fos.write(buffer, 0, len);
                }

                fos.close();   
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

            System.out.println("Done");
        }
        catch(IOException ex)
        {
            ex.printStackTrace(); 
        }
    }    
}

在主类中,我使用了以下代码: 下载 zip 文件后:

try {
    UnZip.main(null);

我得到了这个错误:

file unzip : C:\Level Up! Games\Perfect World\config
   java.io.FileNotFoundException: C:\Level Up! Games\Perfect World\config (Acesso negado)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.spoutcraft.launcher.UnZip.unZipIt(UnZip.java:57)
at org.spoutcraft.launcher.UnZip.main(UnZip.java:20)

有人可以帮我吗?

【问题讨论】:

    标签: java zip unzip


    【解决方案1】:

    我假设在行

    FileOutputStream fos = new FileOutputStream(newFile);  
    

    您正在创建流到代表目录的条目,您应该只创建流到文件

    尝试将循环更改为

    while (ze != null) {
    
        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator
                + fileName);
    
        System.out.println("file unzip : " + newFile);
    
        // create all non exists folders
        // else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {
            FileOutputStream fos = new FileOutputStream(newFile);
    
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
    
            fos.close();
        }
        ze = zis.getNextEntry();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多