是生成了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就是一套加密演算法唄,有點類似於數學的某個公式,你把你的數字輸入到這個公式就會產生另外一個數字,加密就是把你的信息按照一定的方法轉換成別的信息,如果不知道解密方法,是無法理解信息的內容的,大概就是這樣的意思。還不理解請追問