当前位置:首页 » 存储配置 » 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),体积只有原来的几分之一甚至更小。

热点内容
登录界面android 发布:2025-02-02 07:53:23 浏览:840
编译时注解与运行时注解 发布:2025-02-02 07:53:14 浏览:816
怎样登陆ftp 发布:2025-02-02 07:44:44 浏览:631
疯狂点击脚本 发布:2025-02-02 07:38:10 浏览:73
pss算法 发布:2025-02-02 07:30:55 浏览:748
发信息脚本 发布:2025-02-02 07:03:07 浏览:741
l2l3缓存 发布:2025-02-02 06:56:47 浏览:524
为什么安卓下不了虫虫助手 发布:2025-02-02 06:46:47 浏览:45
ftp服务器ui 发布:2025-02-02 06:24:15 浏览:103
wifi有多少种密码 发布:2025-02-02 06:22:06 浏览:587