|
|
Program: How to generate checksum value for for a file in java?
Description: |
A Checksum is used for error checking or to identify whether the file is corrupted while
transferring a file in the network. The data transfer happens in the form of packets. Checksum is computed
on the contents of a file. For each packet the computed checksum value will be different. This computed
value is transmitted along with the packet. At the receiving end we verify the checksum values to determine
whether the file is corrupted or not. Below example shows how to generate checksum value.
|
Code: |
package com.java2novice.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class MyChecksumExample {
public static void main(String a[]){
//Create a zip file using checksum
MyChecksumZip mfe = new MyChecksumZip();
List<String> files = new ArrayList<String>();
files.add("C:/test.txt");
files.add("C:/test.sh");
files.add("C:/port-chn.txt");
mfe.zipFiles(files);
//Unzip the file using checksum
MyChecksumUnzip mcu = new MyChecksumUnzip();
mcu.unzipFile("C:/testing.zip");
}
}
class MyChecksumZip{
public void zipFiles(List<String> files){
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
FileInputStream fis = null;
CheckedOutputStream checksumOs = null;
try {
fos = new FileOutputStream("C:/testing.zip");
checksumOs = new CheckedOutputStream(fos, new Adler32());
zipOut = new ZipOutputStream(new BufferedOutputStream(checksumOs));
for(String filePath:files){
File input = new File(filePath);
fis = new FileInputStream(input);
ZipEntry ze = new ZipEntry(input.getName());
System.out.println("Zipping the file: "+input.getName());
zipOut.putNextEntry(ze);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = fis.read(tmp)) != -1){
zipOut.write(tmp, 0, size);
}
zipOut.flush();
fis.close();
}
zipOut.close();
System.out.println("checksum: "+checksumOs.getChecksum().getValue());
System.out.println("Done... Zipped the files...");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(fos != null) fos.close();
} catch(Exception ex){
}
}
}
}
class MyChecksumUnzip{
public void unzipFile(String filePath){
FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
try {
fis = new FileInputStream(filePath);
CheckedInputStream checksumIs = new CheckedInputStream(fis, new Adler32());
zipIs = new ZipInputStream(new BufferedInputStream(checksumIs));
while((zEntry = zipIs.getNextEntry()) != null){
try{
byte[] tmp = new byte[4*1024];
FileOutputStream fos = null;
String opFilePath = "C:/"+zEntry.getName();
System.out.println("Extracting file to "+opFilePath);
fos = new FileOutputStream(opFilePath);
int size = 0;
while((size = zipIs.read(tmp)) != -1){
fos.write(tmp, 0 , size);
}
fos.flush();
fos.close();
} catch(Exception ex){
}
}
zipIs.close();
System.out.println("Checksum: "+checksumIs.getChecksum().getValue());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|
Output: |
Zipping the file: test.txt
Zipping the file: test.sh
Zipping the file: port-chn.txt
checksum: 108027849
Done... Zipped the files...
Extracting file to C:/test.txt
Extracting file to C:/test.sh
Extracting file to C:/port-chn.txt
Checksum: 108027849
|
|
|
|
|
List of all java.util.zip class sample examples:- How to compress byte array in java?
- How to decompress byte array in java?
- How to zip a single file?
- How to zip multiple files?
- How to read zip files entries or file name list?
- How to unzip files in java?
- How to generate checksum value for for a file in java?
- How to compress and store objects using zip utility?
- How to decompress the compressed objects using zip utility?
- How to zip a file using ZipFile class?
- How to compress a file in GZip format?
- How to uncompress a file from GZip format?
|
|
|
Domain Naming Service(DNS)
It is very difficult to remember a set of numbers (IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome
this problem. It maps one particular IP address to a string of characters. For example, www.java2novice.com implies com is the domain name
reserved for US commercial sites, java2novice is the name of the company and www is the name of the specific computer, which is java2novice's server.
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin
|