-3

When I include fileName and zipParameter in putNextEntry():

ZipOutputStream.putNextEntry(fileName, zipParameter);

It shows an error:

The method putNextEntry(ZipEntry) in the type ZipOutputStream is not applicable for the arguments (String, ZipParameters).
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Can you provide more details? Show more related code. And a bigger stack trace if possible – goamn Apr 01 '19 at 04:26

2 Answers2

1

I hope your fileName wasn’t declared as String. It should be FileOutputStream as first argument, which should get 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();
}

Try it , it may help

Gipsy King
  • 186
  • 2
  • 2
  • 14
  • My fileName isn't declare as String. The problem I'm facing is I want to create a password protected zip, so I search it up online and what they do is that they included both file name and ZipParameters arguments in the putNextEntry() method like what I shown in my question above but it just couldn't work in my code. – Jonathan L. Apr 01 '19 at 04:36
  • @JonathanL. You went online where? And found what? – user207421 Apr 01 '19 at 04:39
  • 1
    @JonathanL. java.util.zip library that has method putNextEntry for ZipOutputStream get only one parameter, which is ZipEntry. – Gipsy King Apr 01 '19 at 04:41
  • Inside the link below is what I've found on how to create a password protected zip. The example three in the link below is what I'm doing now. https://www.programcreek.com/java-api-examples/?api=net.lingala.zip4j.io.ZipOutputStream in the code in example 3. It includes both fileName and ZipParameters arguments, but when i do it this way, it doesn't work. – Jonathan L. Apr 01 '19 at 04:44
  • @JonathanL. That page is clearly labelled 'Java Code Examples for `net.lingala.zip4j.io.ZipOutputStream`'. Clearly that's not what you are using. – user207421 Apr 01 '19 at 04:59
0

Here is the method signature of putNextEntry of java.util.zip.ZipOutputStream which accepts argument as ZipEntry only :

 /**
     * 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
        }
       ....
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
corroborator
  • 321
  • 2
  • 11