java中的zip创建错误

java中的zip创建错误,java,file-handling,zipoutputstream,Java,File Handling,Zipoutputstream,我正在使用ZipOutputStream、FileOutputStream和FileInputStream 首先,我用一个文件创建了一个文件夹。它成功地创建了。然后我尝试创建zip文件。动态地,它第一次正确地创建文件,但第二次,第三次在打开文件时出错 错误:zip[path//file.zip]无法打开该进程无法访问该文件,因为另一进程正在使用该文件。 我用java创建了以下代码 我的代码: demopath+="/myzip"+po.getPoid(); createDir(

我正在使用ZipOutputStream、FileOutputStream和FileInputStream

首先,我用一个文件创建了一个文件夹。它成功地创建了。然后我尝试创建zip文件。动态地,它第一次正确地创建文件,但第二次,第三次在打开文件时出错

错误:zip[path//file.zip]无法打开该进程无法访问该文件,因为另一进程正在使用该文件。 我用java创建了以下代码

我的代码:

 demopath+="/myzip"+po.getPoid();
        createDir(demopath);
        createFileForFamilies("My content", demopath+"/file");
        this.zipDirectory(new File(demopath), demopath+".zip");
我的文件创建者功能:

public String createFileForFamilies(String content, String path) {
    FileOutputStream fop = null;
    File file;
    try {

        file = new File(path);
        fop = new FileOutputStream(file);

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        fop.write(contentInBytes);
        fop.flush();
        fop.close();

        return ("Done");

    } catch (IOException e) {
        System.err.println(e);
        return ("Done");
    } finally {
        try {
            if (fop != null) {
                fop.close();
            }
        } catch (IOException e) {
            System.err.println(e);
            return ("Abort");

        }
    }
}
我的Zip创建功能:

public void zipDirectory(File dir, String zipDirName) {
    try {
        populateFilesList(dir);
        //now zip files one by one
        //create ZipOutputStream to write to the zip file
        FileOutputStream fos = new FileOutputStream(zipDirName);
        ZipOutputStream zos = new ZipOutputStream(fos);
        for (String filePath : filesListInDir) {
            System.out.println("Zipping " + filePath);
            //for ZipEntry we need to keep only relative file path, so we used substring on absolute path
            ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length() + 1, filePath.length()));
            zos.putNextEntry(ze);
            //read the file and write to ZipOutputStream
            FileInputStream fis = new FileInputStream(filePath);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            zos.closeEntry();
            fis.close();

        }
        zos.close();
        fos.close();


    } catch (IOException e) {
        e.printStackTrace();
    }
}

谢谢你,鲍里斯…

这是一个解决方案:

 Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    // locate file system by using the syntax 
    // defined in java.net.JarURLConnection
    URI uri = URI.create("jar:file:/"+zipPath+".zip");

    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        java.nio.file.Path externalTxtFile;
        java.nio.file.Path pathInZipfile ;

        externalTxtFile = Paths.get(gamesPath);
        pathInZipfile = zipfs.getPath("/file.txt");
        Files.copy(externalTxtFile, pathInZipfile,
                StandardCopyOption.REPLACE_EXISTING);
        }
Map env=new HashMap();
环境出售(“创建”、“真实”);
//使用语法定位文件系统
//在java.net.JarURLConnection中定义
URI=URI.create(“jar:file:/”+zipPath+“.zip”);
try(FileSystem-zipfs=FileSystems.newFileSystem(uri,env)){
java.nio.file.Path externalTxtFile;
java.nio.file.Path路径zipfile;
externalTxtFile=path.get(gamesPath);
pathInZipfile=zipfs.getPath(“/file.txt”);
复制(externalTxtFile、pathInZipfile、,
StandardCopyOption。替换_现有);
}

我建议您只需使用一个-它将大大简化您的代码。您建议的页面在Oracle上找不到详细信息抱歉,已修复该链接。看起来您不知何故泄漏了文件句柄。您应该始终使用finally块(或尝试使用java 7/8中的资源)来关闭流之类的东西。@Boris:谢谢,它可以在其他驱动器上工作,但不能在c驱动器上工作。