Zip compression flow local files and decompression

Source: Internet
Author: User
Tags flush

There is a requirement that HTTP gets to the byte array of the zip file and needs to be converted to a local zip or unzip a zip file.


1. Class to use

Import Java.io.FileOutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;
Import Java.io.ByteArrayInputStream;

2 Get the extracted files

private static void Gettxtfile (byte[] data) throws Exception {
		Zipinputstream zipstream = new Zipinputstream (New Bytea Rrayinputstream (data));
		ZipEntry entry = null;
		while ((entry = Zipstream.getnextentry ())!= null) {

			String entryname = Entry.getname ();

			FileOutputStream out = new FileOutputStream ("/log/111/" + entryname);

			byte[] Bytebuff = new byte[4096];
			int bytesread = 0;
			while ((Bytesread = Zipstream.read (bytebuff))!=-1) {
				out.write (bytebuff, 0, bytesread);
			}

			Out.close ();
			Zipstream.closeentry ();
		}
		Zipstream.close ();
}

3. Dump zip file, (can modify the file name in Zip)

private static void Getzipfile (byte[] data throws Exception {
		String filename = "/log/111/111.zip";
		FileOutputStream FileOutputStream = new FileOutputStream (filename);
		Zipoutputstream Zos = new Zipoutputstream (fileoutputstream);

		Zipinputstream ZIPstream = new Zipinputstream (new Bytearrayinputstream (data));
		ZipEntry entry;
		while ((entry = Zipstream.getnextentry ())!= null) {
			ZipEntry entry1 = new ZipEntry (Entry.getname ());
			Zos.putnextentry (entry1);
			Zipstream.closeentry ();
		}
		Zos.write (data);
		Zos.flush ();
		Zos.closeentry ();
		Zos.close ();
		Zipstream.close ();
}

or simply brute-saving it directly to a local file.

private static void Getzipfile (byte[] data throws Exception {
		String filename = "/log/111/111.zip";
		File TargetFile = new file (filename);
		OutputStream OutStream = new FileOutputStream (targetfile);
		Outstream.write (data);
		Outstream.flush ();
		Outstream.close ();
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.