「java压缩解压」java压缩解压第三方

博主:adminadmin 2023-01-14 19:51:05 448

今天给各位分享java压缩解压的知识,其中也会对java压缩解压第三方进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

安装java解压缩核心文件失败怎么办

java压缩文件解压失败

java压缩文件解压失败_java安装 解压缩核心文件失败

第一步:下载 JDK

从 SUN 网站下载 JDK6 或以上版本,这里以 jdk-6u2-windows-i589-p 版为例。

第二步:安装 JDK

(1):双击 jdk-6u2-windows-i589-p.exe 文件,我们e69da5e6ba9062616964757a686964616f31333238666332这里安装路径为:D:\common\Java

(2):安装完成过后,JDK 文件夹包括:

D:\common\Java\jdk1.6.0_02:是 JDK 的安装路径;

bin:binary 的简写,下面存放的是 Java 的各种可执行文件;

db:JDK6 新加入的 Apache 的 Derby 数据库,支持 JDBC4.0 的规范;

include:需要引入的一些头文件,主要是 c 和 c++的,JDK 本身是通过 C 和 C++实现的;

jre:Java 运行环境;

lib:library 的简写,JDK 所需要的一些资源文件和资源包。

第三步:配置环境变量

安装完成后,还要进行 Java 环境的配置,才能正常使用,步骤如下:

(1):在我的电脑点击右键——〉选择属性,

(2):在弹出界面上:选择高级——〉环境变量,

(3):在系统变量里面找到“Path”这一项,然后双击它,在弹出的界面上,在变量值开头添加如下语句“D:\common\Java\jdk1.6.0_02\bin;”,注意不要忘了后面的分号,

(4):然后点击编辑系统变量界面的确定按钮,然后点击环境变量界面的“新建”,

(5):在上面填写变量名为:JAVA_HOME,变量值为:D:\common\Java\jdk1.6.0_02;,注意分号。

(6):然后点击新建系统变量界面的确定按钮,然后点击环境变量界面的“新建”,弹出新建系统变量界面,在上面填写变量名为:classpath ,变量值为:.; ,注意是点和分号。

(7):然后点击一路点击确定按钮,到此设置就完成了。

那么为何要设置这些环境变量呢,如何设置呢:

PATH:提供给操作系统寻找到 Java 命令工具的路径。通常是配置到 JDK 安装路径\bin,如:D:\common\Java\jdk1.6.0_02\bin;。

JAVA_HOME:提供给其它基于 Java 的程序使用,让它们能够找到 JDK 的位置。通常配置到 JDK 安装路径,如:D:\common\Java\jdk1.6.0_02;。注意:JAVA_HOME必须书写正确,全部大写,中间用下划线。

CLASSPATH:提供程序在运行期寻找所需资源的路径,比如:类、文件、图片等等。

注意:在 windows 操作系统上,最好在 classpath 的配置里面,始终在前面保持“.;”的配置,在 windows 里面“.”表示当前路径。

第四步:检测安装配置是否成功

进行完上面的步骤,基本的安装和配置就好了,怎么知道安装成功没有呢?

点击开始——〉点击运行,在弹出的对话框中输入“cmd”,然后点击确定,在弹出的 dos 窗口里面,输入“javac”,然后回车,出现如下界面则表示安装配置成功。

好了,现在 Java 的开发环境就配置好了,接下来就可以进入java的第一个程序了。

javazip压缩包过大解压失败

javazip压缩包过大解压失败的原因:网络传输不好导致文件下载损坏、网站提供的RAR压缩包最初被损坏、使用的下载工具不够完善。我们可以通过压缩软件里的“修复压缩文件”解决javazip压缩包过大解压失败的问题。

怎样用java快速实现zip文件的压缩解压缩

package zip;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Enumeration;

import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

import org.apache.commons.lang3.StringUtils;

public class ZipUtil {

/**

 * 递归压缩文件夹

 * @param srcRootDir 压缩文件夹根目录的子路径

 * @param file 当前递归压缩的文件或目录对象

 * @param zos 压缩文件存储对象

 * @throws Exception

 */

private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception

{

if (file == null) 

{

return;

}

//如果是文件,则直接压缩该文件

if (file.isFile())

{

int count, bufferLen = 1024;

byte data[] = new byte[bufferLen];

//获取文件相对于压缩文件夹根目录的子路径

String subPath = file.getAbsolutePath();

int index = subPath.indexOf(srcRootDir);

if (index != -1) 

{

subPath = subPath.substring(srcRootDir.length() + File.separator.length());

}

ZipEntry entry = new ZipEntry(subPath);

zos.putNextEntry(entry);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

while ((count = bis.read(data, 0, bufferLen)) != -1) 

{

zos.write(data, 0, count);

}

bis.close();

zos.closeEntry();

}

//如果是目录,则压缩整个目录

else 

{

//压缩目录中的文件或子目录

File[] childFileList = file.listFiles();

for (int n=0; nchildFileList.length; n++)

{

childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());

zip(srcRootDir, childFileList[n], zos);

}

}

}

/**

 * 对文件或文件目录进行压缩

 * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径

 * @param zipPath 压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹

 * @param zipFileName 压缩文件名

 * @throws Exception

 */

public static void zip(String srcPath, String zipPath, String zipFileName) throws Exception

{

if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(zipPath) || StringUtils.isEmpty(zipFileName))

{

throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);

}

CheckedOutputStream cos = null;

ZipOutputStream zos = null;

try

{

File srcFile = new File(srcPath);

//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)

if (srcFile.isDirectory()  zipPath.indexOf(srcPath)!=-1) 

{

throw new ParameterException(ICommonResultCode.INVALID_PARAMETER, "zipPath must not be the child directory of srcPath.");

}

//判断压缩文件保存的路径是否存在,如果不存在,则创建目录

File zipDir = new File(zipPath);

if (!zipDir.exists() || !zipDir.isDirectory())

{

zipDir.mkdirs();

}

//创建压缩文件保存的文件对象

String zipFilePath = zipPath + File.separator + zipFileName;

File zipFile = new File(zipFilePath);

if (zipFile.exists())

{

//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException

SecurityManager securityManager = new SecurityManager();

securityManager.checkDelete(zipFilePath);

//删除已存在的目标文件

zipFile.delete();

}

cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());

zos = new ZipOutputStream(cos);

//如果只是压缩一个文件,则需要截取该文件的父目录

String srcRootDir = srcPath;

if (srcFile.isFile())

{

int index = srcPath.lastIndexOf(File.separator);

if (index != -1)

{

srcRootDir = srcPath.substring(0, index);

}

}

//调用递归压缩方法进行目录或文件压缩

zip(srcRootDir, srcFile, zos);

zos.flush();

}

catch (Exception e) 

{

throw e;

}

finally 

{

try

{

if (zos != null)

{

zos.close();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

/**

 * 解压缩zip包

 * @param zipFilePath zip文件的全路径

 * @param unzipFilePath 解压后的文件保存的路径

 * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含

 */

@SuppressWarnings("unchecked")

public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception

{

if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(unzipFilePath))

{

throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);

}

File zipFile = new File(zipFilePath);

//如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径

if (includeZipFileName)

{

String fileName = zipFile.getName();

if (StringUtils.isNotEmpty(fileName))

{

fileName = fileName.substring(0, fileName.lastIndexOf("."));

}

unzipFilePath = unzipFilePath + File.separator + fileName;

}

//创建解压缩文件保存的路径

File unzipFileDir = new File(unzipFilePath);

if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())

{

unzipFileDir.mkdirs();

}

//开始解压

ZipEntry entry = null;

String entryFilePath = null, entryDirPath = null;

File entryFile = null, entryDir = null;

int index = 0, count = 0, bufferSize = 1024;

byte[] buffer = new byte[bufferSize];

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

ZipFile zip = new ZipFile(zipFile);

EnumerationZipEntry entries = (EnumerationZipEntry)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 = new File(entryDirPath);

//如果文件夹路径不存在,则创建文件夹

if (!entryDir.exists() || !entryDir.isDirectory())

{

entryDir.mkdirs();

}

//创建解压文件

entryFile = new File(entryFilePath);

if (entryFile.exists())

{

//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException

SecurityManager securityManager = new SecurityManager();

securityManager.checkDelete(entryFilePath);

//删除已存在的目标文件

entryFile.delete();

}

//写入文件

bos = new BufferedOutputStream(new FileOutputStream(entryFile));

bis = new BufferedInputStream(zip.getInputStream(entry));

while ((count = bis.read(buffer, 0, bufferSize)) != -1)

{

bos.write(buffer, 0, count);

}

bos.flush();

bos.close();

}

}

public static void main(String[] args) 

{

String zipPath = "d:\\ziptest\\zipPath";

String dir = "d:\\ziptest\\rawfiles";

String zipFileName = "test.zip";

try

{

zip(dir, zipPath, zipFileName);

catch (Exception e)

{

e.printStackTrace();

}

String zipFilePath = "D:\\ziptest\\zipPath\\test.zip";

String unzipFilePath = "D:\\ziptest\\zipPath";

try 

{

unzip(zipFilePath, unzipFilePath, true);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

java压缩解压的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java压缩解压第三方、java压缩解压的信息别忘了在本站进行查找喔。