我想更新位於zip文件內的文本文件的內容。修改zipfile條目的文件內容
我無法找到如何做到這一點,下面的代碼無法正常工作。
感謝您的幫助!
import java.util.zip.ZipFile
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
String zipFileFullPath = "C:/path/to/myzipfile/test.zip"
ZipFile zipFile = new ZipFile(zipFileFullPath)
ZipEntry entry = zipFile.getEntry ("someFile.txt")
if(entry){
InputStream input = zipFile.getInputStream(entry)
BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"))
String s = null
StringBuffer sb = new StringBuffer()
while ((s=br.readLine())!=null){
sb.append(s)
}
sb.append("adding some text..")
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileFullPath))
out.putNextEntry(new ZipEntry("someFile.txt"));
int length
InputStream fin = new ByteArrayInputStream(sb.toString().getBytes("UTF8"))
while((length = fin.read(sb)) > 0)
{
out.write(sb, 0, length)
}
out.closeEntry()
}
感謝您的幫助蛋白石。你的第一個解決方案看起來不錯,但由於某種原因沒有任何反應,zip文件似乎根本沒有改變。 – user955732 2014-10-04 14:20:22
壓縮文件不會被刪除。臨時文件看起來不錯。 – user955732 2014-10-04 14:52:32
我已經在Mac OS上測試過。請讓我知道它是否解決了問題。我可以再檢查一次。 – Opal 2014-10-04 22:40:49