如何解决putNextEntry(ZipEntry)不接受ZipParameters的问题?

时间:2019-04-01 04:16:05

标签: java zipoutputstream zip4j

当我在fileName中包含zipParameterputNextEntry()时:

ZipOutputStream.putNextEntry(fileName, zipParameter);

显示错误:

The method putNextEntry(ZipEntry) in the type ZipOutputStream is not applicable for the arguments (String, ZipParameters).

2 个答案:

答案 0 :(得分:1)

希望您的文件名未声明为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();
}

尝试一下,可能会有帮助

答案 1 :(得分:0)

这是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
        }
       ....