㈠ java怎麼讀取Zip和RAR裡面的文件啊
ZipInputStream是一個指向ZIP文件的流,這個流最重要的方法就是getNextEntry方法,一個zip文件可以包含好幾個被壓縮的文件,這個方法的功能就是返回下一個目錄項,也就是返回zip文件中的下一項,並且把流指向這個目錄文件項。getNextEntry的返回值是ZipEntry,它表示zip文件中的一個項,它可以返回這個文件項的大小、名稱等。你可以根據它返回的文件大小調用ZipInputStream的read方法來讀取需要的位元組。給你一個例子:public class ZipTest {
public static void main(String args[]) throws FileNotFoundException, IOException{
ZipInputStream zis = new ZipInputStream(new FileInputStream ("c://a.zip"));//生成讀取ZIP文件的流
ZipEntry ze = zis.getNextEntry();//取得下一個文件項
long size = ze.getSize();//取得這一項的大小
FileOutputStream fos = new FileOutputStream("c://"+ze.getName());//產生輸出文件對象
for(int i= 0;i<size;i++){//循環讀取文件並寫入輸出文件對象
byte c = (byte)zis.read();
fos.write(c);
}
fos.close();
zis.close();
}
}
㈡ 如何用java讀取客戶端上傳的rar文件
直接通過工具類進行解壓或者壓縮文件即可。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;
/**
* 解壓Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration<? extends ZipEntry> entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}
/**
* 將輸入流中的數據寫到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) > 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}
/**
* 安全關閉多個流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
㈢ java中讀取壓縮包中的文件怎麼不用通過解壓
你好,我不知道你說的dzp是什麼格式文件,但如果是zip的壓縮文件,可以看下我的這段代碼
ZipFile file = new ZipFile("d:\\1.zip");
ZipEntry entry = file.getEntry("1.xml"); //假如壓縮包里的文件名是1.xml
InputStream in=file.getInputStream(entry);
最後就是按照java中一貫的流的處理方式即可
㈣ 如何用java讀取zip文件名和zip內文件的文件名,在線等
public static void te(File f) throws IOException {
if (!f.exists() || !f.isDirectory()) {
return;
}
File[] subFiles = f.listFiles();
ZipFile zipFile = null;
for (int i = 0, ii = subFiles == null ? 0 : subFiles.length; i < ii; i++) {
if (subFiles[i].isFile()) {
try {
zipFile = new ZipFile(subFiles[i]);
Enumeration entries = zipFile.entries();
System.out.println("壓縮文件:" + subFiles[i].getAbsolutePath());
while(entries.hasMoreElements())
{
System.out.println(" entry:" + ((ZipEntry)entries.nextElement()).getName());
}
zipFile.close();
} catch (ZipException e) {
//System.out.println(e.getMessage());
}
}
}
}
㈤ Java獲取壓縮包文件列表
public static void main (String args[]) throws IOException {
ZipFile zf = new ZipFile("E:\\test.zip");
InputStream in = new BufferedInputStream(new FileInputStream("E:\\test.zip"));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
} else {
System.err.println("file - " + ze.getName() + " : "
+ ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
System.out.println();
}
}
zin.closeEntry();
}
㈥ 怎麼從壓縮文件中讀取文件並顯示文件中的內容 java編寫
思路是這樣的:
1、將壓縮文件解壓縮到臨時目錄
2、讀取臨時目錄中的文件或者文件夾(如果是文件夾則讀取文件夾中的文件,以此類推)
3、將讀取的內容顯示
4、刪除臨時文件夾中的文件或者文件夾
這些都沒有難點,你覺得對你來說難度在哪裡呢
㈦ 怎麼通過url用java直接讀取zip類型的文件
最簡單的方法。用RadioButton控制項就行了。何苦搞得這么累呢。如果真要這樣做也可以就是五個控制項共用一個事件同時訂閱相同事件然後再判斷你選中的Checkbox其他的為假代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
㈧ java讀取壓縮文件並壓縮
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class JZip {
public static int iCompressLevel;//壓縮比 取值范圍為0~9
public static boolean bOverWrite;//是否覆蓋同名文件 取值范圍為True和False
@SuppressWarnings("unchecked")
private static ArrayList AllFiles = new ArrayList();
public static String sErrorMessage;
private String zipFilePath;
public List<File> srcMap;
public JZip () {
iCompressLevel = 9;
// bOverWrite=true;
}
public JZip(String zipFilePath) throws FileNotFoundException, IOException {
this.zipFilePath = zipFilePath;
}
@SuppressWarnings("unchecked")
public static ArrayList extract (String sZipPathFile , String sDestPath) {
ArrayList AllFileName = new ArrayList();
try {
//先指定壓縮檔的位置和檔名,建立FileInputStream對象
FileInputStream fins = new FileInputStream(sZipPathFile);
//將fins傳入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
AllFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
AllFiles.clear();
return AllFileName;
}
@SuppressWarnings({ "unchecked", "static-access" })
public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {
try {
String sPath;
//先指定壓縮檔的位置及檔名,建立一個FileOutputStream
FileOutputStream fos = new FileOutputStream(sZipPathFile);
//建立ZipOutputStream並將fos傳入
ZipOutputStream zos = new ZipOutputStream(fos);
//設置壓縮比
zos.setLevel(iCompressLevel);
if (bIsPath == true) {
searchFiles(sPathFile);
sPath = sPathFile;
} else {
File myfile = new File(sPathFile);
sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1);
AllFiles.add(myfile);
}
Object[] myobject = AllFiles.toArray();
ZipEntry ze = null;
//每個檔案要壓縮,都要透過ZipEntry來處理
FileInputStream fis = null;
BufferedReader in = null;
//byte[] ch = new byte[256];
for (int i = 0 ; i < myobject.length ; i++) {
File myfile = (File) myobject[i];
if (myfile.isFile()) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"));
//以檔案的名字當Entry,也可以自己再加上額外的路徑
//例如ze=new ZipEntry("test\\"+myfiles[i].getName());
//如此壓縮檔內的每個檔案都會加test這個路徑
ze = new ZipEntry(myfile.getPath().substring((sPath).length()));
//將ZipEntry透過ZipOutputStream的putNextEntry的方式送進去處理
fis = new FileInputStream(myfile);
zos.putNextEntry(ze);
int len = 0;
//開始將原始檔案讀進ZipOutputStream
while ((len = in.read()) != -1) {
zos.write(len);
}
fis.close();
zos.closeEntry();
}
}
zos.close();
fos.close();
AllFiles.clear();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Compress error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
}
/*
這是一個遞歸過程,功能是檢索出所有的文件名稱
dirstr:目錄名稱
*/
@SuppressWarnings("unchecked")
private static void searchFiles (String dirstr) {
File tempdir = new File(dirstr);
if (tempdir.exists()) {
if (tempdir.isDirectory()) {
File[] tempfiles = tempdir.listFiles();
for (int i = 0 ; i < tempfiles.length ; i++) {
if (tempfiles[i].isDirectory())
searchFiles(tempfiles[i].getPath());
else {
AllFiles.add(tempfiles[i]);
}
}
} else {
AllFiles.add(tempdir);
}
}
}
public String getZipFilePath() {
return zipFilePath;
}
public void setZipFilePath(String zipFilePath) {
this.zipFilePath = zipFilePath;
}
/**
* 解析zip文件得到文件名
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean parserZip() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
try {
srcMap = new ArrayList<File>();
while ((entry = zis.getNextEntry()) != null) {
File file = new File(zipFilePath + File.separator + entry.getName());
srcMap.add(file);
}
zis.close();
fis.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
*
* @param zipFileName 待解壓縮的ZIP文件
* @param extPlace 解壓後的文件夾
*/
public static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
File files = new File(extPlace);
files.mkdirs();
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(files + entryName);
file.mkdirs();
System.out.println("創建文件夾:" + entryName);
} else {
OutputStream os = new FileOutputStream(files+File.separator + entryName);
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
System.out.println("解壓文件:" + entryName);
}
}
} catch (IOException e) {
}
}
@SuppressWarnings("static-access")
public static void main(String args[]){
}
}
㈨ java如何讀取壓縮包中的文本文件
壓縮包的里的文件不能直接讀取,只能先解壓縮,再讀取。
建議:可以用apache的工具類,先解壓縮成臨時文件,再讀取,最後刪除臨時文件。