读取目录下的文件
解压缩文件
感觉自己写的好复杂,这里需要注意的是 zipOutput.putNextEntry(new ZipEntry(parentPath + "/"));我在这里没有用File.separator,因为用这个,虽然能压缩成功,但是你解压的时候会把文件夹作为文件去处理,会导致错误,开始弄了半天没明白。
读取共享文件夹
要下载jcifs包
package io;
import java.io.File;
public class FileTest {
public void getFiles(String path) {
File fileDir = new File(path);
if (!fileDir.exists()) {
return;
}
findFile(fileDir);
}
public void findFile(File fileDir) {
File[] files = fileDir.listFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()&&file.getName().endsWith(".txt")) {
System.out.println(file.getName());
} else {
findFile(file);
}
}
}
public static void main(String args[]){
FileTest ft = new FileTest();
ft.getFiles("E:/study");
}
}
解压缩文件
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipFileTest {
public void extractZip(String inPath, String outPath) {
InputStream is = null;
OutputStream os = null;
try {
ZipFile inFile = new ZipFile(inPath);
Enumeration fileHeaders = inFile.entries();
for (; fileHeaders.hasMoreElements();) {
ZipEntry zip = (ZipEntry) fileHeaders.nextElement();
String filename = zip.getName();
String outFilePath = outPath + System.getProperty("file.separator") + filename;
File file = new File(outFilePath);
if (zip.isDirectory()) {
file.mkdirs();
continue;
}
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
is = inFile.getInputStream(zip);
os = new FileOutputStream(outFilePath);
int len = -1;
byte[] buff = new byte[4096];
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);
}
}
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (os != null) {
os.flush();
os.close();
os = null;
}
if (is != null) {
is.close();
is = null;
}
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void compressZip(String inPath, String outPath) {
ZipOutputStream zipOutput = null;
try {
zipOutput = new ZipOutputStream(new FileOutputStream(outPath));
File file = new File(inPath);
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
findFile(file, zipOutput, "");
} else {
zipOutput.putNextEntry(new ZipEntry(file.getName()));
if (file.isFile()) {
InputStream is = new FileInputStream(file);
int len = -1;
byte[] buff = new byte[4069];
while ((len = is.read(buff)) != -1) {
zipOutput.write(buff, 0, len);
}
}
}
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
zipOutput.close();
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void findFile(File fileDir, ZipOutputStream zipOutput, String parentPath) {
File[] files = fileDir.listFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
InputStream is = null;
try {
zipOutput.putNextEntry(new ZipEntry(parentPath + File.separator + file.getName()));
is = new FileInputStream(file);
int len = -1;
byte[] buff = new byte[4069];
while ((len = is.read(buff)) != -1) {
zipOutput.write(buff, 0, len);
}
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
} else {
try {
if (parentPath.isEmpty()) {
parentPath = fileDir.getName();
} else {
parentPath += File.separator + file.getName();
}
zipOutput.putNextEntry(new ZipEntry(parentPath + "/"));
findFile(file, zipOutput, parentPath);
if( parentPath.lastIndexOf(File.separator)>0)
parentPath = parentPath.substring(0, parentPath.lastIndexOf(File.separator));
} catch (IOException ex) {
Logger.getLogger(ZipFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String args[]) {
ZipFileTest zip = new ZipFileTest();
zip.compressZip("E:/study/j2se/io/root", "E:/study/j2se/io/extract/new.zip");
zip.extractZip("E:/study/j2se/io/extract/new.zip", "E:/study/j2se/io/extract/New folder");
}
}
感觉自己写的好复杂,这里需要注意的是 zipOutput.putNextEntry(new ZipEntry(parentPath + "/"));我在这里没有用File.separator,因为用这个,虽然能压缩成功,但是你解压的时候会把文件夹作为文件去处理,会导致错误,开始弄了半天没明白。
读取共享文件夹
package io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class ReaderShareFile {
public void read() {
{
BufferedReader reader = null;
try {
SmbFile smbFile = new SmbFile("smb://username:password@192.168.10.4/Users/Administrator/Desktop/jack/test.txt");
if (smbFile.exists()) {
InputStream input = new SmbFileInputStream(smbFile);
reader = new BufferedReader(new InputStreamReader(input));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}
} catch (IOException ex) {
Logger.getLogger(ReaderShareFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(ReaderShareFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String args[]) {
ReaderShareFile test = new ReaderShareFile();
test.read();
}
}
要下载jcifs包