日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java压缩

發(fā)布時(shí)間:2023/12/3 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java压缩 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在最近的項(xiàng)目中,我們不得不做一些我個(gè)人從未真正看過(guò)的事情。 壓縮。 我們需要拍幾個(gè)文件和圖像,將它們壓縮并提供給FTP使用,是的,總有一天,感覺(jué)確實(shí)回到了90年代。 除了過(guò)去的FTP之行外,它還是一個(gè)很好的機(jī)會(huì),可以花一點(diǎn)時(shí)間在這個(gè)問(wèn)題上。

壓縮檔案

因此,在通常的IO類BufferedInputStream , FileOutputStream和File上面有:

  • ZipInputStream –輸入流,用于讀取ZIP文件格式的文件。 與ZipFile不同,不緩存Zip條目。
  • ZipOutputStream –用于以ZIP文件格式寫(xiě)入文件的輸出流。 這有一個(gè)默認(rèn)的內(nèi)部緩沖區(qū)512,可以使用BufferedOutputStream來(lái)增加它。
  • ZipEntry –表示一個(gè)zip文件中的條目。
  • ZipFile –用于從zip文件讀取條目。 條目被緩存。
  • CRC32 –用于計(jì)算數(shù)據(jù)流的CRC-32。

下面的示例顯示了如何在有和沒(méi)有校驗(yàn)和的情況下壓縮和解壓縮文件夾中的文件:

package javaitzen.blog;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream;/*** The Class FileCompressionUtil.*/ public class FileCompressionUtil {private static final String PATH_SEP = "\\";public static final int BUFFER = 2048;private FileCompressionUtil() {}/*** Zip files in path.* * @param zipFileName the zip file name* @param filePath the file path* @throws IOException Signals that an I/O exception has occurred.*/public static void zipFilesInPath(final String zipFileName, final String filePath) throws IOException {final FileOutputStream dest = new FileOutputStream(zipFileName);final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));try {byte[] data = new byte[BUFFER];final File folder = new File(filePath);final List< String > files = Arrays.asList(folder.list());for (String file : files) {final FileInputStream fi = new FileInputStream(filePath + PATH_SEP + file);final BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);out.putNextEntry(new ZipEntry(file));int count;while ((count = origin.read(data, 0, BUFFER)) != -1) {out.write(data, 0, count);}origin.close();fi.close();}} finally {out.close();dest.close();}}/*** Zip with checksum. CRC32* * @param zipFileName the zip file name* @param folderPath the folder path* @return the checksum* @throws IOException Signals that an I/O exception has occurred.*/public static long zipFilesInPathWithChecksum(final String zipFileName, final String folderPath) throws IOException {final FileOutputStream dest = new FileOutputStream(zipFileName);final CheckedOutputStream checkStream = new CheckedOutputStream(dest, new CRC32());final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checkStream));try {byte[] data = new byte[BUFFER];final File folder = new File(folderPath);final List< String > files = Arrays.asList(folder.list());for (String file : files) {final FileInputStream fi = new FileInputStream(folderPath + PATH_SEP + file);final BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);out.putNextEntry(new ZipEntry(file));int count;while ((count = origin.read(data, 0, BUFFER)) != -1) {out.write(data, 0, count);}origin.close();}} finally {out.close();checkStream.close();dest.flush();dest.close();}return checkStream.getChecksum().getValue();}/*** Unzip files to path.* * @param zipFileName the zip file name* @param fileExtractPath the file extract path* @throws IOException Signals that an I/O exception has occurred.*/public static void unzipFilesToPath(final String zipFileName, final String fileExtractPath) throws IOException {final FileInputStream fis = new FileInputStream(zipFileName);final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));try {ZipEntry entry;while ((entry = zis.getNextEntry()) != null) {int count;byte[] data = new byte[BUFFER];final FileOutputStream fos = new FileOutputStream(fileExtractPath + PATH_SEP + entry.getName());final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);while ((count = zis.read(data, 0, BUFFER)) != -1) {dest.write(data, 0, count);}dest.flush();dest.close();}} finally {fis.close();zis.close();}}/*** Unzip files to path with checksum. CRC32* * @param zipFileName the zip file name* @param fileExtractPath the file extract path* @param checksum the checksum* @return true, if checksum matches;* @throws IOException Signals that an I/O exception has occurred.*/public static boolean unzipFilesToPathWithChecksum(final String zipFileName, final String fileExtractPath, final long checksum) throws IOException {boolean checksumMatches = false;final FileInputStream fis = new FileInputStream(zipFileName);final CheckedInputStream checkStream = new CheckedInputStream(fis, new CRC32());final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checkStream));try {ZipEntry entry = null;while ((entry = zis.getNextEntry()) != null) {int count;byte[] data = new byte[BUFFER];final FileOutputStream fos = new FileOutputStream(fileExtractPath + PATH_SEP + entry.getName());final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);while ((count = zis.read(data, 0, BUFFER)) != -1) {dest.write(data, 0, count);}dest.flush();dest.close();}} finally {zis.close();fis.close();checkStream.close();}if(checkStream.getChecksum().getValue() == checksum) {checksumMatches = true;}return checksumMatches;}}

壓縮物件

我們最終沒(méi)有使用對(duì)象壓縮,但無(wú)論如何我還是看了一下。 我做了一些通用的compress / expand util,不知道它是否有用。 我將輸入?yún)?shù)保留為OutputStream和InputStream,因?yàn)閺睦碚撋现v,它可以與從套接字通信到字符串處理的任何流實(shí)現(xiàn)一起使用。

這里使用與壓縮相關(guān)的類:

  • GZIPInputStream –一個(gè)輸入流過(guò)濾器,用于讀取GZIP文件格式的壓縮數(shù)據(jù)。
  • GZIPOutputStream –輸出流過(guò)濾器,用于以GZIP文件格式寫(xiě)入壓縮數(shù)據(jù)。
  • 默認(rèn)內(nèi)部緩沖區(qū)為512,如果需要更多,請(qǐng)使用BufferedOutputStream 。
package javaitzen.blog;import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream;/*** The Class ObjectCompressionUtil.* * @param <T> the generic type of the serializable object to be compressed*/ public class ObjectCompressionUtil<T extends Serializable> {/*** Compress object.* * @param objectToCompress the object to compress* @param outstream the outstream* @return the compressed object* @throws IOException Signals that an I/O exception has occurred.*/public T compressObject(final T objectToCompress, final OutputStream outstream) throws IOException {final GZIPOutputStream gz = new GZIPOutputStream(outstream);final ObjectOutputStream oos = new ObjectOutputStream(gz);try {oos.writeObject(objectToCompress);oos.flush();return objectToCompress;}finally {oos.close();outstream.close();}}/*** Expand object.* * @param objectToExpand the object to expand* @param instream the instream* @return the expanded object* @throws IOException Signals that an I/O exception has occurred.* @throws ClassNotFoundException the class not found exception*/public T expandObject(final T objectToExpand, final InputStream instream) throws IOException,ClassNotFoundException {final GZIPInputStream gs = new GZIPInputStream(instream);final ObjectInputStream ois = new ObjectInputStream(gs);try {@SuppressWarnings("unchecked")T expandedObject = (T) ois.readObject();return expandedObject;} finally {gs.close();ois.close();}}}

參考:來(lái)自Zen的 JCG合作伙伴 Brian的Java壓縮 技術(shù) 。

編碼愉快!

拜倫

相關(guān)文章 :
  • Cajo,用Java完成分布式計(jì)算的最簡(jiǎn)單方法
  • Hibernate映射集合性能問(wèn)題
  • Java Code Geeks Andygene Web原型
  • Servlet 3.0異步處理可將服務(wù)器吞吐量提高十倍

翻譯自: https://www.javacodegeeks.com/2011/05/java-compression.html

總結(jié)

以上是生活随笔為你收集整理的Java压缩的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。