日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java创建的zip没写入权限,java中的zip创建错误

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java创建的zip没写入权限,java中的zip创建错误 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在使用ZipOutputStream,FileOutputStream和FileInputStream.

首先我創建了一個包含一個文件的文件夾它成功創建了.然后我嘗試創建zip文件.動態地,它首次正確創建文件,但第二次,第三次在打開文件時出錯.

Error: zip [path/././file.zip] Cannot open The process cannot access the file because it is being used by another process.

I created following code in 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();

}

}

總結

以上是生活随笔為你收集整理的java创建的zip没写入权限,java中的zip创建错误的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。