當前位置:首頁 » 存儲配置 » java壓縮存儲

java壓縮存儲

發布時間: 2022-03-05 11:52:51

❶ 在java中,什麼是數組的壓縮存儲

數組是一種引用數據類型,數組引用變數只是一個引用,數組元素和數組變數在內存里是分開存放的.
實際的數組元素被存儲在堆(heap)內存中;數組引用變數是一個引用類型的變數,被存儲在棧(stack)內存中.

❷ java 如何壓縮文件

樓主是說解壓了的文件大小隻有33.1MB,但是卻佔了51.2MB的空間嗎?
如果是這個意思的話,那我要告訴樓主,首先這個問題和JAVA沒有關系,根據你的截圖,可以斷定你用的是FAT32文件系統。這只是文件存儲的形式,很正常。
簡單的說,磁碟存儲數據的最小單位是簇,比方說你的簇大小為128K,一個簇只能存放一個文件,然後你的一個文件只有16K,它佔了這個簇,然後還有112K沒有用,是吧。但是那112K就不能再存放其它文件了。如果要存放其它文件,就要佔另一個簇。
樓主,懂了吧,這跟簇的大小有關,但是也不是簇越小越好,簇越小,讀寫性能都有所下降。這是正常現象。如果你非覺得不舒服,那就用NTFS文件系統吧,把壓縮打開,就不會有這種情況了,希望對你有幫助

❸ java實現mysql存儲壓縮文件

資料庫設計時,欄位定義為LONGBLOB類型。
保存時用
File file = new File("文件路徑");
InputStream is = new ByteArrayInputStream(new FileInputStream(file ));
statement.setBlob(1, is, file.length());

❹ java編程:數據壓縮規則

你是不是問如何編寫一個程序來壓縮數據?如果你是想解決這個問題的話,繼續往下看,如果你只是想要應付考試的話就算了。

—— 分割線

JDK本身提供了數據壓縮的一個API

java.util.zip.Deflater.deflateBytes(byte[] b, int off, int len)

以下是我使用的一個例子,有點多,注釋看不懂可以問我,不知道怎麼用可以問我,其他的就算了。

/**
* threshold value for compress
*/
public static final int THRESHOLD = 1200;

/**
* Answer a byte array compressed in the DEFLATER format from bytes.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] compress(byte[] bytes)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);

// Give the compressor the data to compress
compressor.setInput(bytes);
compressor.finish();

// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try
{
bos.close();
}
catch (IOException e)
{
}

// Get the compressed data
byte[] compressedData = bos.toByteArray();
return compressedData;
}

/**
* Answer a byte array that has been decompressed from the DEFLATER format.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] uncompress(byte[] bytes)
{
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(bytes);

// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished())
{
try
{
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e)
{
}
}
try
{
bos.close();
}
catch (IOException e)
{
}

// Get the decompressed data
byte[] decompressedData = bos.toByteArray();
return decompressedData;
}

❺ 求助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;
}

❻ 怎樣用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();
}
}
}

❼ 如何使用java壓縮文件夾成為zip包(最簡單的

import java.io.File;

public class ZipCompressorByAnt {

private File zipFile;

/**
* 壓縮文件構造函數
* @param pathName 最終壓縮生成的壓縮文件:目錄+壓縮文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile);
}

/**
* 執行壓縮操作
* @param srcPathName 需要被壓縮的文件/文件夾
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName);

File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}

Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夾 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); //排除哪些文件或文件夾
zip.addFileset(fileSet);
zip.execute();
}

}

public class TestZip {

public static void main(String[] args) {

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");
zca.compressExe("E:\test1");
}

}

/*如果 出現ant 的 52 51 50 等版本問題 可以去找對應的ant-1.8.2.jar 我開始用的ant-1.10.1.jar 就是這個包版本高了 一直報verson 52 版本問題*/

❽ java 2個int 用位壓縮技術保存到一個int裡面

1=00000001
2=00000010
4=00000100
8=00001000
16=00010000
應該看明白了吧
1=1
2=2
3=1+2
4=4
5=1+4
6=2+4
7=1+2+4
8=8
9=1+8
....

可以了理解為每種病例是通過在2進制中所處位置區別的
1=最後一位
2=倒數第一位
...
那麼多種病例就可以一個int表示了如 00010011 表示病例1+病例2+病例5等

❾ java如何將很長的字元串存儲為壓縮文件

String str = ""; int length = str.length(); List<Integer> index = new ArrayList<Integer>(); for (int i = 0; i < length - 1; i++) { if(str.charAt(i) != str.charAt(i + 1)){ index.add(i); } } if(str.charAt(length - 2) != str.charAt(length - 1)){ index.add(length - 1); } int start = 0; StringBuffer result = new StringBuffer(); for (int i = 0; i < index.size(); i++) { int end = index.get(i) + 1; if(i != 0){ start = index.get(i - 1) + 1; } String temp = str.substring(start, end); result.append(temp.charAt(0)).append(end - start); } System.out.println(result.toString());最終結果是:a6s4c5d7w1s1a3s1d1c1a1s1

❿ java中的壓縮原理是什麼

JRE就是一個壓縮文件,這跟WINRAR用來壓縮文件是一樣的,並不是文件變大了或變小了,只是所有的文件都壓在一個包下,看起來不會亂。壓縮包下的路徑一樣有用。 答案補充 由於計算機處理的信息是以二進制數的形式表示的,因此壓縮軟體就是把二進制信息中相同的字元串以特殊字元標記來達到壓縮的目的。對於成千上萬單調重復的0和1而言,與其一個一個定義000000…111…長長的一串,還不如告訴電腦:「從這個位置開始存儲1117個1」來得簡潔,而且還能大大節約存儲空間。這是一個非常簡單的壓縮的例子。其實,所有的計算機文件歸根結底都是以「1」和「0」的形式存儲的,只要通過合理的數學計算公式,文件的體積都能夠被大大壓縮以達到「數據無損稠密」的效果。總的來說,壓縮可以分為有損和無損壓縮兩種。如果丟失個別的數據不會造成太大的影響,這時忽略它們是個好主意,這就是有損壓縮。有損壓縮廣泛應用於動畫、聲音和圖像文件中,典型的代表就是影碟文件格式mpeg、音樂文件格式mp3和圖像文件格式jpg。但是更多情況下壓縮數據必須准確無誤,人們便設計出了無損壓縮格式,比如常見的zip、rar等。壓縮軟體(compression software)自然就是利用壓縮原理壓縮數據的工具,壓縮後所生成的文件稱為壓縮包(archive),體積只有原來的幾分之一甚至更小。

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:427
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:552
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:738
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:531
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:141
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:227
java駝峰 發布:2025-02-02 09:13:26 瀏覽:646
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:527
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:207
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:721