zips, , , . , " ", , , " ". , , .
import java.io.*;
import java.util.Deque;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipReader {
private final File topZip;
private final Deque<File> chainedPath;
private final File file;
private final boolean isZipped;
public ZipReader(File file){
chainedPath = new LinkedList<File>();
this.file = file;
file = file.getAbsoluteFile();
File f = file;
chainedPath.addFirst(f);
File tempTopZip = null;
while ((f = f.getParentFile()) != null) {
chainedPath.addFirst(f);
try {
if (tempTopZip == null) {
ZipFile zf = new ZipFile(f);
tempTopZip = f;
}
} catch (ZipException ex) {
} catch (IOException ex) {
Logger.getLogger(ZipReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
isZipped = tempTopZip != null;
if(isZipped){
topZip = tempTopZip;
} else {
topZip = file;
}
}
public boolean exists(){
if(!topZip.exists()){
return false;
}
try{
getInputStream().close();
return true;
} catch(IOException e){
return false;
}
}
public boolean canRead(){
return topZip.canRead();
}
public boolean canWrite(){
if(isZipped){
return false;
} else {
return topZip.canWrite();
}
}
private InputStream getFile(Deque<File> fullChain, String zipName, final ZipInputStream zis) throws FileNotFoundException, IOException {
ZipEntry entry;
InputStream zipReader = new InputStream() {
@Override
public int read() throws IOException {
if (zis.available() > 0) {
return zis.read();
} else {
return -1;
}
}
@Override
public void close() throws IOException {
zis.close();
}
};
boolean isZip = false;
while ((entry = zis.getNextEntry()) != null) {
isZip = true;
Deque<File> chain = new LinkedList<File>(fullChain);
File chainFile = null;
while ((chainFile = chain.pollFirst()) != null) {
if (chainFile.equals(new File(zipName + File.separator + entry.getName()))) {
break;
}
}
if (chainFile == null) {
continue;
}
if (chain.isEmpty()) {
return zipReader;
}
ZipInputStream inner = new ZipInputStream(zipReader);
return getFile(fullChain, zipName + File.separator + entry.getName(), inner);
}
if (isZip) {
throw new FileNotFoundException(zipName + " could not be found!");
} else {
throw new IOException(zipName + " is not a zip file!");
}
}
public InputStream getInputStream() throws FileNotFoundException, IOException {
if (!isZipped) {
return new FileInputStream(file);
} else {
return getFile(chainedPath, topZip.getAbsolutePath(), new ZipInputStream(new FileInputStream(topZip)));
}
}
public String getFileContents() throws FileNotFoundException, IOException {
if (!isZipped) {
return FileUtility.read(file);
} else {
return getStringFromInputStream(getInputStream());
}
}
private String getStringFromInputStream(InputStream is) throws IOException {
BufferedReader din = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
try {
String line;
while ((line = din.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException ex) {
throw ex;
} finally {
try {
is.close();
} catch (Exception ex) {
}
}
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ZipReader other = (ZipReader) obj;
return other.file.equals(this.file);
}
@Override
public int hashCode() {
return file.hashCode();
}
}