导航:首页 > 文件处理 > java压缩base64

java压缩base64

发布时间:2023-05-17 05:11:44

java怎样压缩base64编码的字符串

是生成了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") ; // 设置注释
压缩包里有个注释功能,就相当于备注一样,你在压缩一个包的过程中,你可以选择给它写点什么东西,以方便你根据注释想到里面的大概内容。

阅读全文

与java压缩base64相关的资料

热点内容
exe打开指定文件夹 浏览:264
pdf里面怎么去水印 浏览:845
appleid账号加密码 浏览:220
苹果如何下载微仓app 浏览:916
迅雷解压进度为0 浏览:859
解压解惑近义词 浏览:316
压缩比不一样燃烧室不一样 浏览:101
androidbutton左对齐 浏览:172
怎么找到学校的服务器 浏览:368
android状态栏高度是多少 浏览:987
linuxcliphp 浏览:515
萝卜源码如何关闭用户注册验证 浏览:756
苹果手机头条app怎么没有tv 浏览:563
电脑qq文件夹怎么发不出去 浏览:614
解压小游戏测试钻石剑的硬度 浏览:963
java结束函数 浏览:622
打开远程桌面的命令 浏览:836
树莓派如何搭建mqtt服务器 浏览:588
怎么加密w8文件 浏览:610
linuxprogram 浏览:709