Saturday, October 19, 2013

Decompress files from .zip using java.util.zip

Example to decompress multi files from .zip using java.util.zip, with JavaFX UI.

Decompress files from .zip using java.util.zip
Decompress files from .zip using java.util.zip

package javafx_zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Zip extends Application {

File fileSrc;
private static final int bufferSize = 8192;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");

final Label labelFile = new Label();

Button btn = new Button();
btn.setText("Open FileChooser'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

//Set extension filter
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("Zip files (*.zip)", "*.zip");
fileChooser.getExtensionFilters().add(extFilter);
File fileSrc = fileChooser.showOpenDialog(null);

String source = fileSrc.getPath();
String targetUnZipPath = fileSrc.getParent();

try {
//Compress file
FileInputStream fileInputStream =
new FileInputStream(source);
BufferedInputStream bufferedInputStream =
new BufferedInputStream(fileInputStream);
ZipInputStream zipInputStream =
new ZipInputStream(bufferedInputStream);

ZipEntry zipEntry;

String unzippedMsg = "";

while ((zipEntry = zipInputStream.getNextEntry()) != null) {
try {
byte[] buffer = new byte[bufferSize];
String unzippedFile = targetUnZipPath + "/" + zipEntry.getName();
FileOutputStream fileOutputStream =
new FileOutputStream(unzippedFile);
int size;
while ((size = zipInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, size);
}
fileOutputStream.flush();
fileOutputStream.close();
unzippedMsg += unzippedFile + "\n";
} catch (Exception ex) {
}
}
zipInputStream.close();
labelFile.setText("Unzipped files: \n" + unzippedMsg);
} catch (IOException ex) {
Logger.getLogger(JavaFX_Zip.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(btn, labelFile);

StackPane root = new StackPane();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

Thursday, October 17, 2013

JDK 8 in NetBeans IDE 7.4

This screencast demonstrates how to set up NetBeans IDE 7.4 to use JDK 8, together with how to use JDK 8 profile support and lambda expressions.

Java SE 7 Update 45 and Java SE Embedded 7 Update 45 released

Java SE 7 Update 45 released: visit Java SE Downloads page (Java Platform (JDK) 7u45/JDK 7u45 & NetBeans 7.4)

Java SE Embedded 7 Update 45 released: visit Oracle Java SE Embedded Download page

NetBeans IDE 7.4

NetBeans IDE 7.4 extends the advanced HTML5 development support introduced in NetBeans IDE 7.3 to Java EE and PHP application development, while offering new support for hybrid HTML5 application development on the Android and iOS platforms. In addition, this release provides support for working with preview versions of JDK 8, and includes continued enhancements to JavaFX, C/C++ and more.

NetBeans IDE 7.4 is available in English, Brazilian Portuguese, Japanese, Russian, and Simplified Chinese.

Download NetBeans IDE 7.4

Wednesday, October 16, 2013

Compress multi files to .zip

Compress multi files to .zip
Compress multi files to .zip


package javafx_zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Zip extends Application {

File fileSrc;
private static final int bufferSize = 8192;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");

final Label labelFile = new Label();

Button btn = new Button();
btn.setText("Open FileChooser'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

//Set extension filter
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("files (*.*)", "*.*");
fileChooser.getExtensionFilters().add(extFilter);
List<File> fileSrcList = fileChooser.showOpenMultipleDialog(null);

String targetZipFile = fileSrcList.get(0).getParent() + "/compressed.zip";

try {
//Compress file
ZipOutputStream zipOutputStream;

FileOutputStream fileOutputStream =
new FileOutputStream(targetZipFile);
zipOutputStream = new ZipOutputStream(fileOutputStream);

for (File f : fileSrcList) {
String source = f.getPath();
String entryName = f.getName();

ZipEntry zipEntry = new ZipEntry(entryName);
zipOutputStream.putNextEntry(zipEntry);

byte[] buffer = new byte[bufferSize];
try (FileInputStream fileInputStream = new FileInputStream(source)) {
int numberOfByte;

while ((numberOfByte = fileInputStream.read(buffer, 0, bufferSize))
!= -1) {
zipOutputStream.write(buffer, 0, numberOfByte);
}
}
}
zipOutputStream.close();
labelFile.setText("Compressed file saved as: " + targetZipFile);
} catch (IOException ex) {
Logger.getLogger(JavaFX_Zip.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(labelFile, btn);

StackPane root = new StackPane();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


Monday, October 14, 2013

Decompress file (.gz) using java.util.zip, with JavaFX interface

Example to decompress .gz archives using Package java.util.zip, JavaFX File Chooser.

package javafx_zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Zip extends Application {

File fileSrc;
private static final int bufferSize = 8192;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");

final Label labelFile = new Label();

Button btn = new Button();
btn.setText("Open FileChooser'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

//Set extension filter
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("gz files (*.gz)", "*.gz");
fileChooser.getExtensionFilters().add(extFilter);

//Show open file dialog
fileSrc = fileChooser.showOpenDialog(null);

String source = fileSrc.getPath();
//remove ".gz" from the path
String targetUnGzFile = source.substring(0, source.length()-3);

try {
//UnCompress file

GZIPInputStream gZIPInputStream;

FileInputStream fileInputStream =
new FileInputStream(source);
gZIPInputStream = new GZIPInputStream(fileInputStream);

byte[] buffer = new byte[bufferSize];
try (FileOutputStream fileOutputStream = new FileOutputStream(targetUnGzFile)) {
int numberOfByte;

while((numberOfByte = gZIPInputStream.read(buffer, 0, bufferSize))
!= -1){
fileOutputStream.write(buffer, 0, numberOfByte);
}

fileOutputStream.close();
}

labelFile.setText("UnCompressed file saved as: " + targetUnGzFile);

} catch (IOException ex) {
Logger.getLogger(JavaFX_Zip.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(labelFile, btn);

StackPane root = new StackPane();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


Decompress file (.gz) using java.util.zip, with JavaFX interface
Decompress file (.gz) using java.util.zip, with JavaFX interface


Related: Compress file (.gz) using java.util.zip, with JavaFX interface

Sunday, October 13, 2013

Compress file (.gz) using java.util.zip, with JavaFX interface

The Package java.util.zip provides classes for reading and writing the standard ZIP and GZIP file formats. This example demonstrate how to open file with JavaFX FileChooser, and compress to .gz file with java.util.zip.GZIPOutputStream.

package javafx_zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Zip extends Application {

File fileSrc;
private static final int bufferSize = 8192;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");

final Label labelFile = new Label();

Button btn = new Button();
btn.setText("Open FileChooser'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

//Set extension filter
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("files (*.*)", "*.*");
fileChooser.getExtensionFilters().add(extFilter);

//Show open file dialog
fileSrc = fileChooser.showOpenDialog(null);

String source = fileSrc.getPath();
String targetZipFile = source + ".gz";

try {
//Compress file

GZIPOutputStream gZIPOutputStream;

FileOutputStream fileOutputStream =
new FileOutputStream(targetZipFile);
gZIPOutputStream = new GZIPOutputStream(fileOutputStream);

byte[] buffer = new byte[bufferSize];
try (FileInputStream fileInputStream = new FileInputStream(source)) {
int numberOfByte;

while((numberOfByte = fileInputStream.read(buffer, 0, bufferSize))
!= -1){
gZIPOutputStream.write(buffer, 0, numberOfByte);
}
}
gZIPOutputStream.close();

labelFile.setText("Compressed file saved as: " + targetZipFile);

} catch (IOException ex) {
Logger.getLogger(JavaFX_Zip.class.getName()).log(Level.SEVERE, null, ex);
}

}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(labelFile, btn);

StackPane root = new StackPane();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


Compress file using java.util.zip, with JavaFX interface
Compress file using java.util.zip, with JavaFX interface


Related: Decompress file (.gz) using java.util.zip, with JavaFX interface