try
{
System.out.println("Example of ZIP file decompression.");
// Specify file to decompress
String inFileName = "C:/Daten/test32.zip";
// Specify destination where file will be unzipped
String destinationDirectory = "C:/Daten/";
File sourceZipFile = new File(inFileName);
File unzipDestinationDirectory = new File(destinationDirectory);
// Open Zip file for reading
ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
// Create an enumeration of the entries in the zip file
Enumeration zipFileEntries = zipFile.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
System.out.println("Extracting: " + entry);
File destFile =
new File(unzipDestinationDirectory, currentEntry);
// grab file's parent directory structure
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
// extract file if not a directory
if (!entry.isDirectory())
{
BufferedInputStream is =
new BufferedInputStream(zipFile.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest =
new BufferedOutputStream(fos, BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
zipFile.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
import java.awt.Desktop;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import javax.swing.JOptionPane;
//import javax.swing.JOptionPane;
class Test{
private static final int BUFFER = 2048;
public static void main (String[] args) throws Exception {
//////Datensatz von URL laden/////
String datensatz = "GDS100";
String seitenurl = "ftp.ncbi.nih.gov/pub/geo/DATA/SOFT/GDS/"+datensatz+".soft.gz";
//Datensatz downloaden
URL neturl = new URL("ftp://"+seitenurl);
InputStream in = neturl.openConnection().getInputStream();
/////Datensatz speichern/////
// Lokaler Pfad des Datensatzes
String localurl = "C:"+File.separator+"Daten"+File.separator+datensatz+".gz";
//Fileobjekt mit dem Pfad erzeugen und dort den Datensatz speichern
OutputStream out = new FileOutputStream(new File(localurl));
byte[] buffer = new byte[1024];
for (int n;(n = in.read(buffer)) != -1; out.write(buffer, 0, n));
in.close();
out.close();
/////Datensatz entpacken/////
try
{
System.out.println("Example of ZIP file decompression.");
// Specify file to decompress
String inFileName = "C:/Daten/GDS100.soft.gz";
// Specify destination where file will be unzipped
String destinationDirectory = "C:/Daten/";
File sourceZipFile = new File(inFileName);
File unzipDestinationDirectory = new File(destinationDirectory);
// Open Zip file for reading
ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
// Create an enumeration of the entries in the zip file
Enumeration zipFileEntries = zipFile.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
System.out.println("Extracting: " + entry);
File destFile =
new File(unzipDestinationDirectory, currentEntry);
// grab file's parent directory structure
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
// extract file if not a directory
if (!entry.isDirectory())
{
BufferedInputStream is =
new BufferedInputStream(zipFile.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest =
new BufferedOutputStream(fos, BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
zipFile.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}