Initial Commit V2 (Used code from OBWB8m Data Parser)
This commit is contained in:
120
src/GZIP_Compressor.java
Normal file
120
src/GZIP_Compressor.java
Normal file
@@ -0,0 +1,120 @@
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
||||
public class GZIP_Compressor {
|
||||
|
||||
|
||||
// public static byte[] compressGzipFile(File file) {
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// FileInputStream fis = new FileInputStream(file);
|
||||
//// FileOutputStream fos = new FileOutputStream(gzipFile);
|
||||
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
// GZIPOutputStream gzipOS = new GZIPOutputStream(byteArrayOutputStream);
|
||||
//
|
||||
// byte[] buffer = new byte[1024];
|
||||
// int len;
|
||||
//
|
||||
// while((len = fis.read(buffer)) != -1){
|
||||
//// gzipOS.write(buffer, 0, len);
|
||||
// gzipOS.write(buffer);
|
||||
// }
|
||||
// //close resources
|
||||
// gzipOS.close();
|
||||
// fis.close();
|
||||
//
|
||||
// return byteArrayOutputStream.toByteArray();
|
||||
//
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public static byte[] compress(File file) throws IOException {
|
||||
if (file == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
|
||||
DataInputStream reader = new DataInputStream(new FileInputStream(file));
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
|
||||
// gzipOutputStream.write(text.getBytes(StandardCharsets.UTF_8));
|
||||
gzipOutputStream.write(reader.readAllBytes());
|
||||
}
|
||||
reader.close();
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] compress(String text) throws IOException {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
|
||||
gzipOutputStream.write(text.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// String originalText = "This is a sample text to be compressed.";
|
||||
// try {
|
||||
// byte[] compressedData = compress(originalText);
|
||||
// System.out.println("Original text: " + originalText);
|
||||
// System.out.println("Compressed data size: " + compressedData.length + " bytes");
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
class GzipCompression {
|
||||
|
||||
public static byte[] compress(String text) throws IOException {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
|
||||
gzipOutputStream.write(text.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String originalText = "This is a sample text to be compressed.";
|
||||
try {
|
||||
byte[] compressedData = compress(originalText);
|
||||
System.out.println("Original text: " + originalText);
|
||||
System.out.println("Compressed data size: " + compressedData.length + " bytes");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user