Created
May 23, 2015 21:22
-
-
Save Deliquescence/f916f669086b70ae6bba to your computer and use it in GitHub Desktop.
base64 with splitting
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package facebookmessing; | |
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; | |
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.Scanner; | |
import net.lingala.zip4j.core.ZipFile; | |
import net.lingala.zip4j.exception.ZipException; | |
import net.lingala.zip4j.model.ZipParameters; | |
import net.lingala.zip4j.util.Zip4jConstants; | |
/** | |
* | |
* @author Deliquescence <[email protected]> | |
*/ | |
public class FacebookMessing { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws FileNotFoundException, IOException, ZipException { | |
//File to encode | |
File inFile = new File("memes.jpg"); | |
/////////// | |
//ZIPPING// | |
/////////// | |
ZipFile zipFile = new ZipFile("ZipFile.zip"); | |
// Initiate Zip Parameters which define various properties such | |
// as compression method, etc. | |
ZipParameters parameters = new ZipParameters(); | |
// set compression method to store compression | |
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); | |
// Set the compression level. This value has to be in between 0 to 9 | |
parameters.setCompressionLevel(8); | |
// Create a split file by setting splitArchive parameter to true | |
// and specifying the splitLength. SplitLenth has to be greater than | |
// 65536 bytes | |
// Please note: If the zip file already exists, then this method throws an | |
// exception | |
zipFile.createZipFile(inFile, parameters); | |
//////////////////// | |
//ENCODE AND SPLIT// | |
//////////////////// | |
byte[] encoded = encodeFileToBase64(zipFile.getFile()); | |
//spliting | |
final int SPLIT_SIZE = 20000; | |
for (int i = 0; i < encoded.length / SPLIT_SIZE; i++) { | |
//create each file | |
File outFile = new File("base64_" + i + ".txt"); | |
outFile.createNewFile(); | |
FileOutputStream fos = new FileOutputStream(outFile); | |
//encode to base64 and put it in the file | |
fos.write(encoded, i * SPLIT_SIZE, SPLIT_SIZE); | |
} | |
//the end file needs special treatment because its not SPLIT_LENGTH long | |
int i = (encoded.length / SPLIT_SIZE); | |
File outFile = new File("base64_" + i + ".txt"); | |
outFile.createNewFile(); | |
FileOutputStream fos = new FileOutputStream(outFile); | |
//encode to base64 and put it in the file | |
fos.write(encoded, i * SPLIT_SIZE, (encoded.length - (i * SPLIT_SIZE))); | |
////////////////////// | |
//UNSPLIT AND DECODE// | |
////////////////////// | |
ArrayList<File> parts = new ArrayList<>(); | |
int j = 0; | |
while (true) { | |
//get each file | |
File part = new File("base64_" + j + ".txt"); | |
if (part.exists()) {//to figure out how many parts there are | |
parts.add(part); | |
j++; | |
} else { | |
break;//stop adding | |
} | |
} | |
//file to decode | |
File outfileTwo = new File("decoded.zip"); | |
FileOutputStream fos2 = new FileOutputStream(outfileTwo); | |
//init scanner that will scan each part | |
Scanner scan = null; | |
for (File part : parts) { | |
scan = new Scanner(part);//set scanner to each part | |
fos2.write(base64decoder(scan.next().getBytes()));//decode part and add to file | |
} | |
} | |
public static byte[] encodeFileToBase64(File encodeFile) throws FileNotFoundException, IOException { | |
//Input stream for file to encode | |
FileInputStream fis = new FileInputStream(encodeFile); | |
//byte array that the input file will be converted to | |
byte[] mybytes = new byte[(int) encodeFile.length()]; | |
//convert to bytes | |
fis.read(mybytes); | |
return base64encoder(mybytes); | |
} | |
public static byte[] decodeBase64toFile(String decodeString) { | |
return base64decoder(decodeString); | |
} | |
private static byte[] base64encoder(byte[] encodeBytes) { | |
return Base64.encode(encodeBytes).getBytes(); | |
} | |
private static byte[] base64decoder(byte[] decodeBytes) { | |
String decodeString = new String(decodeBytes); | |
return Base64.decode(decodeString); | |
} | |
private static byte[] base64decoder(String decodeString) { | |
return Base64.decode(decodeString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment