当我在fileName
中包括zipParameter
和putNextEntry()
时:
ZipOutputStream.putNextEntry(fileName, zipParameter);
它显示一个错误:
The method putNextEntry(ZipEntry) in the type ZipOutputStream is not applicable for the arguments (String, ZipParameters).
我希望你的fileName没有声明为String。它应该是FileOutputStream作为第一个参数,它应该获取File。
File zipFile = new File("file.txt");
ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(zipFile));
try
{
ZipEntry entry = new ZipEntry("myFile.txt"); // put file inside
zos.putNextEntry(entry);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
zos.closeEntry();
zos.close();
}
尝试一下,它可能有所帮助
以下是java.util.zip.ZipOutputStream的putNextEntry的方法签名,它仅接受ZipEntry
参数:
/**
* Begins writing a new ZIP file entry and positions the stream to the
* start of the entry data. Closes the current entry if still active.
* The default compression method will be used if no compression method
* was specified for the entry, and the current time will be used if
* the entry has no set modification time.
* @param e the ZIP entry to be written
* @exception ZipException if a ZIP format error has occurred
* @exception IOException if an I/O error has occurred
*/
public void putNextEntry(ZipEntry e) throws IOException {
ensureOpen();
if (current != null) {
closeEntry(); // close previous entry
}
....