Base64 to zip java

utsengar / EncodeBased64Binary.java

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 org . apache . commons . codec . binary . Base64 ;
private String encodeFileToBase64Binary ( String fileName )
throws IOException
File file = new File ( fileName );
byte [] bytes = loadFile ( file );
byte [] encoded = Base64 . encodeBase64 ( bytes );
String encodedString = new String ( encoded );
return encodedString ;
>
private static byte [] loadFile ( File file ) throws IOException
InputStream is = new FileInputStream ( file );
long length = file . length ();
if ( length > Integer . MAX_VALUE )
// File is too large
>
byte [] bytes = new byte [( int ) length ];
int offset = 0 ;
int numRead = 0 ;
while ( offset < bytes . length
&& ( numRead = is . read ( bytes , offset , bytes . length — offset )) >= 0 )
offset += numRead ;
>
if ( offset < bytes . length )
throw new IOException ( «Could not completely read file » + file . getName ());
>
is . close ();
return bytes ;
>

Источник

Base64 to zip java

In my project, we run some queries in Solr server, and return combined response back to client. But some text fields are too large, we would like to reduce their size.

Use GZIPOutputStream or ZipOutputStream?

To compress data, Java provides two streams GZIPOutputStream and ZipOutputStream. What’s the difference and which we should use? The compression algorithm and performance is almost same (Lempel-Ziv Welch), the difference is that GZIP format is used to compress a single file, and ZIP is a archive format: to compress many files in a single archive: using putNextEntry and closeEntry to add a new entry into the archive file.

In this case, we use GZIPOutputStream, because we don’t add and compress multiple file in a single archive, so no need to use ZipOutputStream, also the code to use ZipOutputStream si a little simpler.

Use GZIPOutputStream and Base64 Encoder to Compress String At server side, we can use GZIPOutputStream to compress a string to a byte array stored in ByteArrayOutputStream. But we can’t transfer the byte array as text in http response. We have to use a Base64 encoder to encode the byte array as Base64. We can use org.apache.commons.codec.binary.Base64.encodeBase64String(). Then we add the compressed text as a field in Solr Document field — not shown in the code below.

Test Data:

Original text in memory is about 134,479,520(134mb), its zipped byte array is about 9,001,240(9mb), base 64 string is 16,198,528(16mb). We can see that the size reduces 88%. This is huge and it’s worth. Use Base64 Decoder and GZIPInputStream to Uncompress String At remote client side, we first read the text response from stream, about how to read one Solr document using stream API, please read: Solr: Use STAX Parser to Read XML Response to Reduce Memory Usage Solr: Use SAX Parser to Read XML Response to Reduce Memory Usage Solr: Use JSON(GSon) Streaming to Reduce Memory Usage

Then use org.apache.commons.codec.binary.Base64.decodeBase64() to decode the Base64 string to byte array, and then use ZipInputStream to read the zipped byte array to get original unzipped string, then add it to Solr Document as a field.

         zi = result = IOUtils. > IOUtils. > >

Test Code

  args) IOUtils. IOUtils. IOUtils. fis = IOUtils. fw = IOUtils. IOUtils. >

Resource

Источник

Base64 to zip java

Here is the code snippet to create the ZIP file with multiple folders inside it. Each folder has multiple json / xml files inside it. Once zip file got created, convert the zip file into the Base 64 encoded format as a String using Java.

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.codec.binary.Base64; public class ConvertFolderToZip < private ListfileList; private static final String OUTPUT_ZIP_FILE = "test.zip"; private static final String SOURCE_FOLDER = "/Users/Downloads/test-folder"; // SourceFolder path public ConvertFolderToZip() < fileList = new ArrayList(); > public static void main(String[] args) < final ConvertFolderToZip appZip = new ConvertFolderToZip(); appZip.generateFileList(new File(SOURCE_FOLDER)); appZip.zipIt(OUTPUT_ZIP_FILE); // convert the zip file which includes multiple folders inside to base64 encode String encodedData = convertZipFileToBaseEncodeString(); System.out.println("Base64 encoded data : "+ encodedData); >public static String convertZipFileToBaseEncodeString() < final File originalFile = new File(OUTPUT_ZIP_FILE); String encodedBase64 = null; try < final FileInputStream fileInputStreamReader = new FileInputStream(originalFile); final byte[] bytes = new byte[(int)originalFile.length()]; fileInputStreamReader.read(bytes); encodedBase64 = new String(Base64.encodeBase64(bytes)); >catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >return encodedBase64; > public void zipIt(final String zipFile) < final byte[] buffer = new byte[1024]; final String source = new File(SOURCE_FOLDER).getName(); FileOutputStream fos = null; ZipOutputStream zos = null; try < fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(fos); System.out.println("Output to Zip : " + zipFile); FileInputStream in = null; for (String file : this.fileList) < System.out.println("File Added : " + file); final ZipEntry ze = new ZipEntry(source + File.separator + file); zos.putNextEntry(ze); try < in = new FileInputStream(SOURCE_FOLDER + File.separator + file); int len; while ((len = in.read(buffer)) >0) < zos.write(buffer, 0, len); >> finally < in.close(); >> zos.closeEntry(); System.out.println("Folder successfully compressed"); > catch (IOException ex) < ex.printStackTrace(); >finally < try < zos.close(); >catch (IOException e) < e.printStackTrace(); >> > public void generateFileList(File node) < // add file only if (node.isFile()) < fileList.add(generateZipEntry(node.toString())); >if (node.isDirectory()) < String[] subNote = node.list(); for (String filename : subNote) < generateFileList(new File(node, filename)); >> > private String generateZipEntry(String file) < return file.substring(SOURCE_FOLDER.length() + 1, file.length()); >> How to create Zip file in Java Happy Coding !! Keep watching this space for more updates. Have a nice day :-) [polldaddy poll=6894547]

Источник

Base64 to zip java

Here is the code snippet to create the ZIP file with multiple files inside it. Once zip file got created, convert the zip file into the Base 64 encoded format as a String using Java.

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.codec.binary.Base64; public class FileConverter < public static void main(String[] args) throws IOException < ListsrcFiles = Arrays.asList("file1.json", "file2.json"); FileOutputStream fos = new FileOutputStream("multifiles.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String srcFile : srcFiles) < File fileToZip = new File(srcFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) < zipOut.write(bytes, 0, length); >fis.close(); > zipOut.close(); fos.close(); // convert the zip file to the base64 encoded string String encodedData = convertZipFileToBaseEncodeString(); System.out.println("encodedData > " + encodedData); > public static String convertZipFileToBaseEncodeString() < File originalFile = new File("multifiles.zip"); String encodedBase64 = null; try < FileInputStream fileInputStreamReader = new FileInputStream(originalFile); byte[] bytes = new byte[(int) originalFile.length()]; fileInputStreamReader.read(bytes); encodedBase64 = new String(Base64.encodeBase64(bytes)); >catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >return encodedBase64; > > 

Keep watching this space for more updates.

Источник

Base64 to zip java

Here is the code snippet to create the ZIP file with multiple files inside it. Once zip file got created, convert the zip file into the Base 64 encoded format as a String using Java.

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.codec.binary.Base64; public class FileConverter < public static void main(String[] args) throws IOException < ListsrcFiles = Arrays.asList("file1.json", "file2.json"); FileOutputStream fos = new FileOutputStream("multifiles.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String srcFile : srcFiles) < File fileToZip = new File(srcFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) < zipOut.write(bytes, 0, length); >fis.close(); > zipOut.close(); fos.close(); // convert the zip file to the base64 encoded string String encodedData = convertZipFileToBaseEncodeString(); System.out.println("encodedData > " + encodedData); > public static String convertZipFileToBaseEncodeString() < File originalFile = new File("multifiles.zip"); String encodedBase64 = null; try < FileInputStream fileInputStreamReader = new FileInputStream(originalFile); byte[] bytes = new byte[(int) originalFile.length()]; fileInputStreamReader.read(bytes); encodedBase64 = new String(Base64.encodeBase64(bytes)); >catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >return encodedBase64; > > 

Keep watching this space for more updates.

Источник

Читайте также:  Generate html with images
Оцените статью