base 64 Gzip 을 통 해 압축 및 복호화
4940 단어 자바
import java.io.*;
import java.util.zip.*;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64GzipUtils {
public static File base64ToFile(String base64) {
if(base64==null||"".equals(base64)) {
return null;
}
byte[] buff=new Base64().decode(base64.getBytes());
File file=null;
FileOutputStream fout=null;
try {
file = File.createTempFile("tmp", ".pdf",new File("D://"));
fout=new FileOutputStream(file);
fout.write(buff);
fout.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fout!=null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
/**
* Base64
* @param primStr
* @return
*/
public static String zipString(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try{
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(primStr.getBytes("gbk"));
zout.closeEntry();
return new BASE64Encoder().encode(out.toByteArray());
} catch (IOException e) {
return null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// base64 string
/*
* Base64
* @param compressedStr
* @return
*/
public static final String unzipString(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString("gbk");
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return decompressed;
}
public static String file2Base64(File file) {
if(file==null) {
return null;
}
String base64 = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
byte[] buff = new byte[fin.available()];
fin.read(buff);
base64 = new String(new Base64().encode(buff));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
public static void main(String[] args) throws IOException {
Base64GzipUtils zwz = new Base64GzipUtils();
File file = new File("D://1.pdf");
String str = file2Base64(file);
System.out.println(str);
String compString = zipString(str);
System.out.println("========");
System.out.println(compString);
System.out.println(compString.length());
String decompString = unzipString(compString);
// System.out.println(decompString);
base64ToFile(decompString);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.