Home > Java, Mini-tips, snippets, Tips > Como leer archivo Zip o Unzip txt en Java

Como leer archivo Zip o Unzip txt en Java

January 20, 2015 Leave a comment Go to comments

Obtener el archivo de texto en de un archivo zip


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipFile {
	public static void main(String[] args) {
		String fileName = "./files/myZipFile.txt.zip";
		unzipTextFile(fileName);
	}

	public static void unzipTextFile(String zipName) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(zipName);
			ZipInputStream zis = new ZipInputStream(fis);
			ZipEntry ze = zis.getNextEntry();
			InputStreamReader isr = new InputStreamReader(zis);
			BufferedReader reader = new BufferedReader(isr);
			String outFileName = "./files/" + ze.getName();
			//"./files/myZipFile.txt" -> output file
			FileWriter fw = new FileWriter(outFileName);
			String line;
			while ((line = reader.readLine()) != null) {
				// manipula line
				fw.append(line).append('\n');
			}
			fw.flush();
			fw.close();
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Categories: Java, Mini-tips, snippets, Tips
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: