-1

as I can put a password on a zip file grails

package zip
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

class SampleZipController {
def index() { }
def downloadSampleZip() {
    response.setContentType('APPLICATION/OCTET-STREAM')
    response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"')
    ZipOutputStream zip = new ZipOutputStream(response.outputStream);

    def file1Entry = new ZipEntry('first_file.txt');
    zip.putNextEntry(file1Entry);
    zip.write("This is the content of the first file".bytes);   
    def file2Entry = new ZipEntry('second_file.txt');
    zip.putNextEntry(file2Entry);
    zip.write("This is the content of the second file".bytes);
    zip.setPassword("password");

    zip.close();

}
} 

The problem is that I put the setPassword property and does not create any zip file case 'm getting the wrong word reserved.

D.Sanxhez
  • 137
  • 8

1 Answers1

0

OK, here's the deal. The Java SDK does not provide password protection for zip files. ZipOutputStream.setPassword() does not exist, nor does any other method to set a password.

What you can do is use a third-party library instead. Here's a Groovy script demonstrating how to password protect a ZIP file using Winzipaes:

@Grab('de.idyl:winzipaes:1.0.1')

import de.idyl.winzipaes.AesZipFileEncrypter
import de.idyl.winzipaes.impl.AESEncrypterBC

def outputStream = new FileOutputStream('test.zip') /* Create an OutputStream */
def encrypter = new AesZipFileEncrypter(outputStream, new AESEncrypterBC())
def password = 'password'

encrypter.add('first_file.txt', new ByteArrayInputStream('This is the content of the first file'.bytes), password)
encrypter.add('second_file.txt', new ByteArrayInputStream('This is the content of the second file'.bytes), password)
encrypter.close()

As you can see the API is similar to the one you were using. Keep in mind this is a Groovy script without Grails, so ensure to add the Winzipaes dependency appropriately (don't use @Grab).

Note: The encryption algorithm is AES256, which is not supported by all ZIP clients. For example, on Mac OSX I had to install 7-zip to decrypt the file; the built-in ZIP tools did not work.

You can read more about password-protecting ZIP files here.

Community
  • 1
  • 1
Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • thanks for your reply. modify the code so that the .zip can be downloaded but I can not do this could help me please. – D.Sanxhez Feb 24 '16 at 12:43
  • Sure. I already answered the question, so that's done. Just join the Grails Noob Slack community and i can help you figure out the rest of it. It's right here: http://emmanuelrosa.com – Emmanuel Rosa Feb 24 '16 at 14:07