A. java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
Java上傳pdf文件,復只壓縮大小,不改制變成.zip/.rar文件
可以,壓縮只是一種演算法,什麼語言都可以,比如某種格式的文件中1001010(二進制)代表漢子的"中"字,那麼壓縮演算法就是在編碼不沖突的情況下可以改變編碼長度,比如壓縮之後中字變成1010,這樣就節省空間了,這是我隨便舉的例子,具體的對應演算法可以網上查
B. java中怎麼獲得壓縮文件的大小需要獲得壓縮包里的每個文件的大小,求各位幫忙
importjava.io.*;
importjava.util.zip.*;
/**
*@authorHardneedl
*/
publicclassZipEntriesDemo{
publicstaticvoidmain(String...args)throwsIOException{
ZipInputStreamzipIn=newZipInputStream(newFileInputStream(args[0]));
ZipEntryzipEntry;
while((zipEntry=zipIn.getNextEntry())!=null){
zipIn.closeEntry();
if(!zipEntry.isDirectory()){
Stringname=zipEntry.getName();
longsize=zipEntry.getSize();
longcompd=zipEntry.getCompressedSize();
System.out.printf("%s,size=%d,compressedsize=%d ",name,size,compd);
}
}
zipIn.close();
}
}
C. 怎樣用java快速實現zip文件的壓縮解壓縮
packagezip;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
importorg.apache.commons.lang3.StringUtils;
publicclassZipUtil{
/**
*遞歸壓縮文件夾
*@paramsrcRootDir壓縮文件夾根目錄的子路徑
*@paramfile當前遞歸壓縮的文件或目錄對象
*@paramzos壓縮文件存儲對象
*@throwsException
*/
privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException
{
if(file==null)
{
return;
}
//如果是文件,則直接壓縮該文件
if(file.isFile())
{
intcount,bufferLen=1024;
bytedata[]=newbyte[bufferLen];
//獲取文件相對於壓縮文件夾根目錄的子路徑
StringsubPath=file.getAbsolutePath();
intindex=subPath.indexOf(srcRootDir);
if(index!=-1)
{
subPath=subPath.substring(srcRootDir.length()+File.separator.length());
}
ZipEntryentry=newZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));
while((count=bis.read(data,0,bufferLen))!=-1)
{
zos.write(data,0,count);
}
bis.close();
zos.closeEntry();
}
//如果是目錄,則壓縮整個目錄
else
{
//壓縮目錄中的文件或子目錄
File[]childFileList=file.listFiles();
for(intn=0;n<childFileList.length;n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir,childFileList[n],zos);
}
}
}
/**
*對文件或文件目錄進行壓縮
*@paramsrcPath要壓縮的源文件路徑。如果壓縮一個文件,則為該文件的全路徑;如果壓縮一個目錄,則為該目錄的頂層目錄路徑
*@paramzipPath壓縮文件保存的路徑。注意:zipPath不能是srcPath路徑下的子文件夾
*@paramzipFileName壓縮文件名
*@throwsException
*/
publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException
{
if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStreamcos=null;
ZipOutputStreamzos=null;
try
{
FilesrcFile=newFile(srcPath);
//判斷壓縮文件保存的路徑是否為源文件路徑的子文件夾,如果是,則拋出異常(防止無限遞歸壓縮的發生)
if(srcFile.isDirectory()&&zipPath.indexOf(srcPath)!=-1)
{
thrownewParameterException(ICommonResultCode.INVALID_PARAMETER,".");
}
//判斷壓縮文件保存的路徑是否存在,如果不存在,則創建目錄
FilezipDir=newFile(zipPath);
if(!zipDir.exists()||!zipDir.isDirectory())
{
zipDir.mkdirs();
}
//創建壓縮文件保存的文件對象
StringzipFilePath=zipPath+File.separator+zipFileName;
FilezipFile=newFile(zipFilePath);
if(zipFile.exists())
{
//檢測文件是否允許刪除,如果不允許刪除,將會拋出SecurityException
=newSecurityManager();
securityManager.checkDelete(zipFilePath);
//刪除已存在的目標文件
zipFile.delete();
}
cos=newCheckedOutputStream(newFileOutputStream(zipFile),newCRC32());
zos=newZipOutputStream(cos);
//如果只是壓縮一個文件,則需要截取該文件的父目錄
StringsrcRootDir=srcPath;
if(srcFile.isFile())
{
intindex=srcPath.lastIndexOf(File.separator);
if(index!=-1)
{
srcRootDir=srcPath.substring(0,index);
}
}
//調用遞歸壓縮方法進行目錄或文件壓縮
zip(srcRootDir,srcFile,zos);
zos.flush();
}
catch(Exceptione)
{
throwe;
}
finally
{
try
{
if(zos!=null)
{
zos.close();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
/**
*解壓縮zip包
*@paramzipFilePathzip文件的全路徑
*@paramunzipFilePath解壓後的文件保存的路徑
*@paramincludeZipFileName解壓後的文件保存的路徑是否包含壓縮文件的文件名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
publicstaticvoinzip(StringzipFilePath,StringunzipFilePath,booleanincludeZipFileName)throwsException
{
if(StringUtils.isEmpty(zipFilePath)||StringUtils.isEmpty(unzipFilePath))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
FilezipFile=newFile(zipFilePath);
//如果解壓後的文件保存路徑包含壓縮文件的文件名,則追加該文件名到解壓路徑
if(includeZipFileName)
{
StringfileName=zipFile.getName();
if(StringUtils.isNotEmpty(fileName))
{
fileName=fileName.substring(0,fileName.lastIndexOf("."));
}
unzipFilePath=unzipFilePath+File.separator+fileName;
}
//創建解壓縮文件保存的路徑
FileunzipFileDir=newFile(unzipFilePath);
if(!unzipFileDir.exists()||!unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}
//開始解壓
ZipEntryentry=null;
StringentryFilePath=null,entryDirPath=null;
FileentryFile=null,entryDir=null;
intindex=0,count=0,bufferSize=1024;
byte[]buffer=newbyte[bufferSize];
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
ZipFilezip=newZipFile(zipFile);
Enumeration<ZipEntry>entries=(Enumeration<ZipEntry>)zip.entries();
//循環對壓縮包里的每一個文件進行解壓
while(entries.hasMoreElements())
{
entry=entries.nextElement();
//構建壓縮包中一個文件解壓後保存的文件全路徑
entryFilePath=unzipFilePath+File.separator+entry.getName();
//構建解壓後保存的文件夾路徑
index=entryFilePath.lastIndexOf(File.separator);
if(index!=-1)
{
entryDirPath=entryFilePath.substring(0,index);
}
else
{
entryDirPath="";
}
entryDir=newFile(entryDirPath);
//如果文件夾路徑不存在,則創建文件夾
if(!entryDir.exists()||!entryDir.isDirectory())
{
entryDir.mkdirs();
}
//創建解壓文件
entryFile=newFile(entryFilePath);
if(entryFile.exists())
{
//檢測文件是否允許刪除,如果不允許刪除,將會拋出SecurityException
=newSecurityManager();
securityManager.checkDelete(entryFilePath);
//刪除已存在的目標文件
entryFile.delete();
}
//寫入文件
bos=newBufferedOutputStream(newFileOutputStream(entryFile));
bis=newBufferedInputStream(zip.getInputStream(entry));
while((count=bis.read(buffer,0,bufferSize))!=-1)
{
bos.write(buffer,0,count);
}
bos.flush();
bos.close();
}
}
publicstaticvoidmain(String[]args)
{
StringzipPath="d:\ziptest\zipPath";
Stringdir="d:\ziptest\rawfiles";
StringzipFileName="test.zip";
try
{
zip(dir,zipPath,zipFileName);
}
catch(Exceptione)
{
e.printStackTrace();
}
StringzipFilePath="D:\ziptest\zipPath\test.zip";
StringunzipFilePath="D:\ziptest\zipPath";
try
{
unzip(zipFilePath,unzipFilePath,true);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
D. java 怎麼把文件壓縮
用Eclipse可以將java文件打成.jar包,再用exe4打成exe文件,不過exe4貌似不是免費的,打成jar包的具體過程是File->Export->java->jar file->next->選擇工程中的java文件->下面選擇jar包的路徑->next->main class禮選擇主類->finish,就ok 了,注意,只有你的機子上安裝了jre/jdk之後打出來的才是jar包,直接點擊才能運行,不然打出來的是壓縮包,也就是說jar包只有在裝了java運行環境的機子上才可以運行
E. java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變
也就是圖片壓縮,可以不修改任何大小的壓縮(速度快),也可等比例修改大小壓縮(較慢)
下面這是一段等比例縮小圖片的方法。
public String compressPic(String inputDir, String outputDir,
String inputFileName, String outputFileName, int width,
int height, boolean gp,String hzm) {
try {
if (!image.exists()) {
return "";
}
Image img = ImageIO.read(image);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
return "no";
} else {
int newWidth; int newHeight;
// 判斷是否是等比縮放
if (gp == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null)) / (double) width ;
double rate2 = ((double) img.getHeight(null)) / (double) height ;
// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = img.getWidth(null); // 輸出的圖片寬度
newHeight = img.getHeight(null); // 輸出的圖片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的縮略演算法 生成縮略圖片的平滑度的
* 優先順序比速度高 生成的圖片質量比較好 但速度慢
*/
Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(im, 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
//JPEGImageEncoder可適用於其他圖片類型的轉換
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
F. javazip壓縮包過大解壓失敗
javazip壓縮包過大解壓失敗的原因:網路傳輸不好導致文件下載損壞、網站提供的RAR壓縮包最初被損壞、使用的下載工具不夠完善。我們可以通過壓縮軟體里的「修復壓縮文件」解決javazip壓縮包過大解壓失敗的問題。
G. 求助java壓縮圖片存儲大小的方法
可以使用Draw這個類,通過改變像素來改變存儲大小,實例如下:
(StringsrcFilePath,StringdescFilePath)throwsIOException{
Filefile=null;
BufferedImagesrc=null;
FileOutputStreamout=null;
ImageWriterimgWrier;
ImageWriteParamimgWriteParams;
//指定寫圖片的方式為jpg
imgWrier=ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams=newjavax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
//要使用壓縮,必須指定壓縮方式為MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
//這里指定壓縮的程度,參數qality是取值0~1范圍內,
imgWriteParams.setCompressionQuality((float)1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModelcolorModel=ImageIO.read(newFile(srcFilePath)).getColorModel();//ColorModel.getRGBdefault();
//指定壓縮時使用的色彩模式
//imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
//colorModel,colorModel.createCompatibleSampleModel(16,16)));
imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
colorModel,colorModel.createCompatibleSampleModel(16,16)));
try{
if(isBlank(srcFilePath)){
returnfalse;
}else{
file=newFile(srcFilePath);System.out.println(file.length());
src=ImageIO.read(file);
out=newFileOutputStream(descFilePath);
imgWrier.reset();
//必須先指定out值,才能調用write方法,ImageOutputStream可以通過任何
//OutputStream構造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
//調用write方法,就可以向輸入流寫圖片
imgWrier.write(null,newIIOImage(src,null,null),
imgWriteParams);
out.flush();
out.close();
}
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
publicstaticbooleanisBlank(Stringstring){
if(string==null||string.length()==0||string.trim().equals("")){
returntrue;
}
returnfalse;
}
H. java 如何壓縮文件
樓主是說解壓了來的文件大小隻源有33.1MB,但是卻佔了51.2MB的空間嗎?
如果是這個意思的話,那我要告訴樓主,首先這個問題和JAVA沒有關系,根據你的截圖,可以斷定你用的是FAT32文件系統。這只是文件存儲的形式,很正常。
簡單的說,磁碟存儲數據的最小單位是簇,比方說你的簇大小為128K,一個簇只能存放一個文件,然後你的一個文件只有16K,它佔了這個簇,然後還有112K沒有用,是吧。但是那112K就不能再存放其它文件了。如果要存放其它文件,就要佔另一個簇。
樓主,懂了吧,這跟簇的大小有關,但是也不是簇越小越好,簇越小,讀寫性能都有所下降。這是正常現象。如果你非覺得不舒服,那就用NTFS文件系統吧,把壓縮打開,就不會有這種情況了,希望對你有幫助
I. 如何使用JAVA代碼壓縮PDF文件
用java代碼壓縮應用到程序了,代碼一般是比較復雜的,對pdf文件的mate標簽優化,這類標簽包括三類,pdf文件不是網頁就是個文件,何況我們可以用pdf壓縮工具壓縮,下面有個解決方法,樓主可以做參照。
1:點擊打開工具,打開主頁面上有三個功能進行選擇,我們選擇pdf文件壓縮。
J. java中的壓縮原理是什麼
什麼是壓縮文件?
簡單的說,就是經過壓縮軟體壓縮的文件叫壓縮文件,壓縮的原理是把文件的二進制代碼壓縮,把相鄰的0,1代碼減少,比如有000000,可以把它變成6個0
的寫法60,來減少該文件的空間。
■怎麼壓縮文件?
首先要安裝壓縮軟體,現在比較流行的是WinRAR「一種高效快速的文件壓縮軟體(中文版)」。
其次是建立一個壓縮包:選擇你要製作成壓縮包的文件或文件夾,當然你也可也多選,方法同資源管理器,也就是按住Ctrl或Shift再選擇文件(文件夾)。
選取完畢之後,就可以單擊工具欄上的「壓縮」按鈕,在這里你可以選擇壓縮格式:RAR和ZIP。
如果你想得到較大的壓縮率,建議選擇RAR格式。
各個選項選擇好以後,單擊確定按鈕就開始製作壓縮包了,非常方便。有時候大家會遇到這個問題,就是你在一個論壇里要上傳一些文件壓縮包,壓縮包大小有3M,但是論壇限制會員上傳大小隻有2M,怎麼辦呢?
其實辦法很簡單,就是在你壓縮這個文件時,分成幾個帶分卷壓縮包,分卷包大小設置為2M即可,比如:原來文件名為123.rar(3M),壓縮成分卷包後為123.part1.rar(2M)與123.part2.rar(1M)兩個文件,這樣你就可以上傳了。
具體方法如下:
1、在要壓縮的文件上點右鍵
2、添加到壓縮文件....
3、選常規
4、壓縮方式選最好
5、批定壓縮分卷大小(按位元組計算),1M
=
1024K,1K
=
1024位元組,填寫數字即可
當你下載了帶有分卷的壓縮包後,如何解壓文件呢?
具體方法如下:
1、把所有的壓縮分卷全部下載完整
2、所有分卷必須在同一個文件夾內
3、然後雙擊解壓第一個分卷,即可
註:分卷解壓的文件必須是連續的,若分卷未下載完整,則解壓時自然會提示需要下一壓縮分卷