是生成了base64字元串,要壓縮 ? 那可以使用zip輸出流去壓縮
~~~~~
Ⅱ Base64 編碼和解碼 java=》C#
io流部分好像有點區別
其他部分貌似不用改了
Ⅲ java讀取壓縮文件並壓縮
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class JZip {
public static int iCompressLevel;//壓縮比 取值范圍為0~9
public static boolean bOverWrite;//是否覆蓋同名文件 取值范圍為True和False
@SuppressWarnings("unchecked")
private static ArrayList AllFiles = new ArrayList();
public static String sErrorMessage;
private String zipFilePath;
public List<File> srcMap;
public JZip () {
iCompressLevel = 9;
// bOverWrite=true;
}
public JZip(String zipFilePath) throws FileNotFoundException, IOException {
this.zipFilePath = zipFilePath;
}
@SuppressWarnings("unchecked")
public static ArrayList extract (String sZipPathFile , String sDestPath) {
ArrayList AllFileName = new ArrayList();
try {
//先指定壓縮檔的位置和檔名,建立FileInputStream對象
FileInputStream fins = new FileInputStream(sZipPathFile);
//將fins傳入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
AllFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
AllFiles.clear();
return AllFileName;
}
@SuppressWarnings({ "unchecked", "static-access" })
public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {
try {
String sPath;
//先指定壓縮檔的位置及檔名,建立一個FileOutputStream
FileOutputStream fos = new FileOutputStream(sZipPathFile);
//建立ZipOutputStream並將fos傳入
ZipOutputStream zos = new ZipOutputStream(fos);
//設置壓縮比
zos.setLevel(iCompressLevel);
if (bIsPath == true) {
searchFiles(sPathFile);
sPath = sPathFile;
} else {
File myfile = new File(sPathFile);
sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1);
AllFiles.add(myfile);
}
Object[] myobject = AllFiles.toArray();
ZipEntry ze = null;
//每個檔案要壓縮,都要透過ZipEntry來處理
FileInputStream fis = null;
BufferedReader in = null;
//byte[] ch = new byte[256];
for (int i = 0 ; i < myobject.length ; i++) {
File myfile = (File) myobject[i];
if (myfile.isFile()) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"));
//以檔案的名字當Entry,也可以自己再加上額外的路徑
//例如ze=new ZipEntry("test\\"+myfiles[i].getName());
//如此壓縮檔內的每個檔案都會加test這個路徑
ze = new ZipEntry(myfile.getPath().substring((sPath).length()));
//將ZipEntry透過ZipOutputStream的putNextEntry的方式送進去處理
fis = new FileInputStream(myfile);
zos.putNextEntry(ze);
int len = 0;
//開始將原始檔案讀進ZipOutputStream
while ((len = in.read()) != -1) {
zos.write(len);
}
fis.close();
zos.closeEntry();
}
}
zos.close();
fos.close();
AllFiles.clear();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Compress error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
}
/*
這是一個遞歸過程,功能是檢索出所有的文件名稱
dirstr:目錄名稱
*/
@SuppressWarnings("unchecked")
private static void searchFiles (String dirstr) {
File tempdir = new File(dirstr);
if (tempdir.exists()) {
if (tempdir.isDirectory()) {
File[] tempfiles = tempdir.listFiles();
for (int i = 0 ; i < tempfiles.length ; i++) {
if (tempfiles[i].isDirectory())
searchFiles(tempfiles[i].getPath());
else {
AllFiles.add(tempfiles[i]);
}
}
} else {
AllFiles.add(tempdir);
}
}
}
public String getZipFilePath() {
return zipFilePath;
}
public void setZipFilePath(String zipFilePath) {
this.zipFilePath = zipFilePath;
}
/**
* 解析zip文件得到文件名
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean parserZip() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
try {
srcMap = new ArrayList<File>();
while ((entry = zis.getNextEntry()) != null) {
File file = new File(zipFilePath + File.separator + entry.getName());
srcMap.add(file);
}
zis.close();
fis.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
*
* @param zipFileName 待解壓縮的ZIP文件
* @param extPlace 解壓後的文件夾
*/
public static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
File files = new File(extPlace);
files.mkdirs();
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(files + entryName);
file.mkdirs();
System.out.println("創建文件夾:" + entryName);
} else {
OutputStream os = new FileOutputStream(files+File.separator + entryName);
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
System.out.println("解壓文件:" + entryName);
}
}
} catch (IOException e) {
}
}
@SuppressWarnings("static-access")
public static void main(String args[]){
}
}
Ⅳ 在Java中如何進行BASE64編碼和解碼
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}
//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}
Ⅳ 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%。
Ⅶ java壓縮文件的問題
如果只從你給的代碼來看的話
file.getName()這個方法就是來自最開頭地方引入的文件import java.util.zip.ZipEntry ;
zipOut.setComment("www.mldnjava.cn") ; // 設置注釋
壓縮包里有個注釋功能,就相當於備注一樣,你在壓縮一個包的過程中,你可以選擇給它寫點什麼東西,以方便你根據注釋想到裡面的大概內容。