java创建的zip没写入权限,java中的zip创建错误
我正在使用ZipOutputStream,FileOutputStream和FileInputStream.
首先我創(chuàng)建了一個包含一個文件的文件夾它成功創(chuàng)建了.然后我嘗試創(chuàng)建zip文件.動態(tài)地,它首次正確創(chuàng)建文件,但第二次,第三次在打開文件時出錯.
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");
我的文件創(chuàng)建者功能:
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創(chuàng)建功能:
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();
}
}
總結(jié)
以上是生活随笔為你收集整理的java创建的zip没写入权限,java中的zip创建错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想拯救者Y9000P 2022上新:冰
- 下一篇: 网络主播范思峰偷逃税被罚649.5万:半