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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值