是生成了base64字符串,要压缩 ? 那可以使用zip输出流去压缩
~~~~~
❷ JAVA压缩至32K以下后的图片base64码
Java实现图片与Base64编码互转
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
publicclassBase64Image{
publicstaticvoidmain(String[]args){
//测试从Base64编码转换为图片文件
StringstrImg="自己写哈";
GenerateImage(strImg,"D:\wangyc.jpg");
//测试从图片文件转换为Base64编码
System.out.println(GetImageStr("d:\wangyc.jpg"));
}
publicstaticStringGetImageStr(StringimgFilePath){//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
byte[]data=null;
//读取图片字节数组
try{
InputStreamin=newFileInputStream(imgFilePath);
data=newbyte[in.available()];
in.read(data);
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoderencoder=newBASE64Encoder();
returnencoder.encode(data);//返回Base64编码过的字节数组字符串
}
(StringimgStr,StringimgFilePath){//对字节数组字符串进行Base64解码并生成图片
if(imgStr==null)//图像数据为空
returnfalse;
BASE64Decoderdecoder=newBASE64Decoder();
try{
//Base64解码
byte[]bytes=decoder.decodeBuffer(imgStr);
for(inti=0;i<bytes.length;++i){
if(bytes[i]<0){//调整异常数据
bytes[i]+=256;
}
}
//生成jpeg图片
OutputStreamout=newFileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
returntrue;
}catch(Exceptione){
returnfalse;
}
}
}
❸ 把纯文本字符串用Gzip压缩再转换为Base64能有多少压缩率
其实具体多大压缩率要看源文件的内容,一般来说重复的单词越多,压缩率越高。
下面是把/usr/share/dict/words压缩的测试程序
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64;
public class GzipBase64Tests {
public static void main(String[] args) throws Exception {
File input = new File("/Users/matianyi/input.txt");
File output = new File("/Users/matianyi/output.txt");
if (!input.exists()) {
System.out.println("input file not exists!");
return;
}
if (output.exists()) {
output.delete();
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(buffer);
FileInputStream in = new FileInputStream(input);
long t1 = System.currentTimeMillis();
byte[] buf = new byte[1024];
int total=0;
int rd;
while ((rd = in.read(buf)) != -1) {
total += rd;
gout.write(buf,0, rd);
}
gout.close();
in.close();
byte[] result = buffer.toByteArray();
long t2 = System.currentTimeMillis();
String base64 = Base64.encodeBase64String(result);
long t3 = System.currentTimeMillis();
System.out.printf("raw %d -> gzip %d -> base64 %d, time1 %dms, time2 %dms", total, result.length, base64.length(), t2-t1, t3-t2);
}
}
输出为: raw 2493109 -> gzip 753932 -> base64 1005244, time1 225ms, time2 43ms
压缩了50%。
❹ 如何在前端解压缩获得的base64Binary字符串
应该对整个输出进行gzip压缩,同时设置好Content-Encoding
gzip,而不是单单压缩部分返回的内容,部分压缩浏览器不会加压
自己看http://download.csdn.net/download/bljbljbljblj/2744049这个类库是否能解压,不能自己按照gzip原理自己写代码了,要不就不要压缩
❺ 如何有效提高Base64编码后的文件的压缩比
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
// 初始化工作 , initialize
// 定义查询表 , = 是为了原码不够分配而加上的。
char sz64Table[65] = {'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z','a','b','c','d',
'e','f','g','h','i',
'j','k','l','m','n',
'o','p','q','r','s',
't','u','v','w','x',
'y','z','0','1','2',
'3','4','5','6','7',
'8','9','+','/','='
} ;
// 定义 变换函数。
void Encode64 (string strGetStr) // 转换
{
// 判断其长度是否符合要求
int nNumberOfGetStr = 0 ; // 串的长度
int nNeedByte ; // 需要补充的字节数
int nGroup ; // 分组数
nNumberOfGetStr = strGetStr.length ();
if ( nNumberOfGetStr == 0 )
{
cout << "输入串为空!"<< endl;
exit ( 1 ) ;
}
else // 长度不为空,要判断数量是否需要补充
{
nGroup = nNumberOfGetStr / 3 ; // 这里是分组数的初步值
nNeedByte = nNumberOfGetStr % 3 ; // 这里是一个模,用的时候还得注意。
if ( nNeedByte == 1 ) // 模1 说明需要两个
{
nGroup ++ ;
strGetStr.append ("00") ;
}
if ( nNeedByte == 2 ) // 模2说明需要一个补0
{
nGroup ++ ;
strGetStr.append ("0") ;
}
} // if
// 开始分组 , 也就是对每一组都进行变换。
char szTemp[3] ; // 中间过渡
// char * p = new char( nGroup * 4 ); // 因为转换后容量是原来的4/3
char p[300] ;
for ( int i = 0 ; i < nGroup ; i ++ )
{
szTemp[0] = strGetStr.at ( i * 3 + 0) ;
szTemp[1] = strGetStr.at ( i * 3 + 1) ;
szTemp[2] = strGetStr.at ( i * 3 + 2) ;
//第一个输出字节:取第一输入字节的前6位,并且在高位补0,使其变成8位(一个字节)
p[i * 4 + 0] = szTemp[0] >> 2 ;
//第二个输出字节:取第一输入字节的后2位和第二个输入字节的前4位(共6位),并且在高位补0,使其变成8位(一个字节)
p[i * 4 + 1] = ((szTemp[0] & 0x03) << 4) ^ (szTemp[1] >> 4) ;
//第三个输出字节:取第二输入字节的后4位和第三个输入字节的前2位(共6位),并且在高位补0,使其变成8位(一个字节)
if ( szTemp[1] == 48) p[i * 4 + 2] = 64 ; // 48 也就是 0 的ASCII
else p[i * 4 + 2] = ((szTemp[1] & 0x0f) << 2) ^ (szTemp[2] >> 6) ;
//第四个输出字节:取第三输入字节的后6位,并且在高位补0,使其变成8位(一个字节)
if ( szTemp[2] == 48) p[i * 4 + 3] = 64 ;
else p[i * 4 + 3] = szTemp[2] & 0x3f ;
}
for (int j = 0 ; j < nGroup * 4 ; j ++)
{
cout << sz64Table[ p[j] ] ;
}
cout << endl;
}// Encode is over ;
昨天看着一个C#代码改写的,可能有些语法语义的不正确,大家见谅。水平实在是不行,但我写了之后发现如果是对一个字符进行编码的话,会出现问题。
跟其他程序比起来,差了3个位置。比如: 1 编为 :MQ== , 我的编为: MT== 。字符多了就没问题。自己没想明白,谁给审审!
❻ java 压缩 iso-8859-1 base64 哪个字节小
这两个都是编码,谈不上压缩。
iso-8859-1 是西欧的语言编码
base64 是通用的一种编码。
❼ java问题:一个字符串base64解码后再zip解压
importorg.apache.commons.codec.binary.Base64;
publicclassc{
publicstaticvoidmain(String[]args)throwsException{
//Stringbase64Str=
//"UEsDBC0AAAAIAAaPJkfS5clx//////////+/mU+//////////AAAABAAEASwAAANUAAAAAAA==";
//BASE64Decoderdecoder=newBASE64Decoder();
//byte[]b=decoder.decodeBuffer(base64Str);//解码
//Stringresult=decompressByteArrayToString(b,"UTF-8");
//System.out.println(result);
StringbaseString=Base64.encodeBase64String("我爱中国".getBytes("UTF-8"));
System.out.println(""我爱中国"的Base64编码为:"+baseString);
Stringbase64Str="5oiR54ix5Lit5Zu9";
byte[]bytes=Base64.decodeBase64(base64Str);
System.out.println("解码后:"+newString(bytes,"UTF-8"));
}
}
❽ Base64 编码和解码 java=》C#
io流部分好像有点区别
其他部分貌似不用改了
❾ 你好,最近我也遇到用java压缩和解压,向你咨询下你的解决方案什么,你是怎么用文件流方式去压缩
package com.onewaveinc.cwds.commons.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author fz 2010-7-30
* @Description 把指定文件夹下的所有文件压缩为指定文件夹下指定zip文件;把指定文件夹下的zip文件解压到指定目录下
*/
public class ZipUtils {
private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
private static final String SEPARATE = "/";
/**
* @Author fz 2010-7-30
* @param sourceDir 待压缩目录
* @param zipFile 压缩文件名称
* @throws Exception
* @Description 把sourceDir目录下的所有文件进行zip格式的压缩,保存为指定zip文件
*/
public static void zip(String sourceDir, String zipFile) throws Exception {
OutputStream os = null;
// try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
// 直接压缩单个文件时,取父目录
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
// } catch (Exception e) {
// logger.error("压缩文件或文件夹" + sourceDir + "时发生异常");
// e.printStackTrace();
// }
}
/**
* @Author fz 2010-7-30
* @param source 源文件
* @param basePath 待压缩文件根目录
* @param zos 文件压缩流
* @Description 执行文件压缩成zip文件
*/
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
//存相对路径(相对于待压缩的根目录)
String pathName = null;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + SEPARATE;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
logger.error("压缩文件" + source + "时发生异常");
e.printStackTrace();
}
}
/**
* @Author fz 2010-7-30
* @param zipfile 待解压文件
* @param destDir 解压文件存储目录
* @throws Exception
* @Description 解压zip文件,只能解压zip文件
*/
@SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir) throws Exception {
destDir = destDir.endsWith(SEPARATE) ? destDir : destDir + SEPARATE;
byte b[] = new byte[1024];
int length;
ZipFile zipFile;
// try {
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists()) {
loadFile.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
outputStream.close();
inputStream.close();
}
}
zipFile.close();
// } catch (IOException e) {
// logger.error("解压文件" + zipfile + "时发生异常");
// e.printStackTrace();
// }
}
}
❿ Java里,,什么是二进制 什么是base64他们有什么区别
二进制就是逢二进一,而我们习惯的是十进制,就是逢十进一,其实就是平时计数的时候用的符号不一样,十进制是(0~9)10个符号,而二进制只有0和1两个,因为当超过1的时候就会产生进位,比如二进制加法1+1=10(注意不要和十进制的十混淆,这个是二进制一零,等于十进制的二)。
至于base64就是一套加密算法呗,有点类似于数学的某个公式,你把你的数字输入到这个公式就会产生另外一个数字,加密就是把你的信息按照一定的方法转换成别的信息,如果不知道解密方法,是无法理解信息的内容的,大概就是这样的意思。还不理解请追问