Created
January 9, 2012 11:57
-
-
Save yanaga/1582650 to your computer and use it in GitHub Desktop.
Simple ZipOutputStream Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
public class JavaZip { | |
public static void main(String[] args) throws Exception { | |
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(args[0])); | |
for (int i = 1; i < args.length; i++) { | |
zipOutputStream.putNextEntry(new ZipEntry(args[i])); | |
FileInputStream inputStream = new FileInputStream(args[i]); | |
for (int read = 0; (read = inputStream.read()) != -1;) { | |
zipOutputStream.write(read); | |
} | |
zipOutputStream.closeEntry(); | |
} | |
zipOutputStream.close(); | |
} | |
} |
Salve Klaus, é possível sim. Só é inviável se o arquivo for muito grande. Sugiro usar:
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
Depois é só pegar o(s) ZipEntry(ies) e escrever para um OutputStream. Se for na memória, um ByteArrayOutputStream, por exemplo.
Mas como pego o conteudo de cada ZipEntry? Nao tenho q dar ZipFile.getInputStream(ZipEntry)? Tem outro jeito sem ter o file?
Klaus, criei um novo gist com um exemplo: https://gist.github.com/1596238
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mano, me dá uma luz. É possivel recuperar os arquivos de dentro de um jar (zip) se tenho somente ele como stream, sem ter q primeiro escrever seu conteúdo inteiro para um arquivo pra depois extrair?