簡體   English   中英

Java中的zip創建錯誤

[英]zip creation error in java

我正在使用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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM