顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2013年12月17日 星期二

Java 解開ZIP檔案

奮戰了幾天,雖然實作了JSP在線解開ZIP檔案的功能
但是有個致命的缺點
ZIP檔的檔案名稱不能含有非ASCII的字元(ISO-8859-1)
一遇上非ISO-8859-1編碼的字串
下面的程式就會停擺
google一下有個package可以解決這個問題
目前還在研究這個package
有收穫再貼上來分享
先看看下面簡易的在線解壓功能
老實說,效能不怎好
解相同的檔案較PHP及C#都要來的慢..


package com.jIAn.decompress;
import java.lang.*;
import java.io.*;
import java.util.zip.*;
public class zip
{
    public zip(){}
    public void DecompressFile(String arg_zipFileName, String arg_outputDirectory) throws Exception
    {
        ZipInputStream objZipInputStream = new ZipInputStream(new FileInputStream(arg_zipFileName));
        ZipEntry objZipEntry;
        InputStream objInputStream = null;
        File objFile ;
        int intStreamLength ;        
        while ((objZipEntry = objZipInputStream.getNextEntry()) != null) 
        {
            
            if (objZipEntry.isDirectory()) 
            {
                String strDirectoryName = objZipEntry.getName();
                strDirectoryName = strDirectoryName.substring(0, strDirectoryName.length() - 1);
                objFile = new File(arg_outputDirectory + File.separator + strDirectoryName);
                objFile.mkdir();
                System.out.println("建立資料夾 " + arg_outputDirectory + File.separator + strDirectoryName);
            }
            else 
            {
                objFile = new File(arg_outputDirectory + File.separator + objZipEntry.getName());
                objFile.createNewFile();
                FileOutputStream objFileOutputStream = new FileOutputStream(objFile);                
                while ((intStreamLength = objZipInputStream.read()) != -1)
                {
                    objFileOutputStream.write(intStreamLength);
                }
                objFileOutputStream.close();
            }
            System.out.println("解壓 " + objZipEntry.getName()+".......ok");
        }
        objZipInputStream.close();
    }

}
如何使用?
建立物件
zip objZip = new zip();
objZip.DecompressFile(欲解開的Zip檔,解到那一個目錄);
objZip.DecompressFile("C:/zip/123.zip","C:\\zip\\123");
還有一個要注意的地方
如果解壓到多層路徑需要先行建立多層路徑的目錄

Java DES 範例


package com.jIAn.crypt;
import javax.crypto.*;

public class DES
{
    private static String strDefaultKey = "jIAn";    //預設的金鑰
    private StringBuffer objSb             = null;
    private Cipher objCipher             = null;
    private java.security.Key objKey    = null;
    private int intStringLength            = 0;
    private int intTemp                    = 0;
    
    //預設建構子
    public DES() throws Exception
    {        
        this(strDefaultKey);
    }

    //自訂密鑰
    public DES(String arg_strKey) throws Exception 
    {
        setKey(arg_strKey.getBytes());
        objCipher = Cipher.getInstance("DES");
    }

    //從指定的字串製成密鑰,密鑰所需的字元陣列長度為8位,不足及超過都要處理
     private void setKey(byte[] arg_strPrivateKey) throws Exception 
     {         
         byte[] arrTempByteArray = new byte[8];
         // 將原始字元陣列轉換為8位
         for (int i = 0; i < arg_strPrivateKey.length && i < arrTempByteArray.length; i++)
         {
             arrTempByteArray[i] = arg_strPrivateKey[i];
         }
         // 設定密鑰
         objKey = new javax.crypto.spec.SecretKeySpec(arrTempByteArray, "DES");    
     }
     
     //將byte陣列轉換16進制值的字串,如:byte[]{1,18}轉換為:0112     
    public String byte2Hex(byte[] arg_bteArray) throws Exception 
    {
        intStringLength = arg_bteArray.length;    
        objSb = new StringBuffer(intStringLength * 2);
        for (int i = 0; i < intStringLength; i++)
        {
            intTemp = (int)arg_bteArray[i];
            //負數需要轉成正數
            if(intTemp < 0) 
            {
                intTemp = intTemp + 256;
            }
            // 小於0F需要補0
            if (intTemp < 16)
            {
                objSb.append("0");
            }
            objSb.append(Integer.toString(intTemp, 16));
        }
        return objSb.toString();
     }

    
    //將16進制值的字串轉成byte陣列        
    public byte[] hex2Byte(String arg_strHexString) throws Exception 
    {
        byte[] arrByteDAta = arg_strHexString.getBytes();
        intStringLength = arrByteDAta.length;
        byte[] aryRetuenData = new byte[intStringLength / 2];
        for (int i = 0; i < intStringLength; i = i + 2)
        {
            aryRetuenData[i / 2] =  (byte)Integer.parseInt(new String(arrByteDAta, i, 2), 16);
        }
        return aryRetuenData;
    }

    //加密字串
     public byte[] doEncrypt(byte[] arg_bteArray) throws Exception
     {
         objCipher.init(Cipher.ENCRYPT_MODE, objKey);
         return objCipher.doFinal(arg_bteArray);
     }
     
     public String encrypt(String arg_strToEncriptString) throws Exception 
     {
         return byte2Hex(doEncrypt(arg_strToEncriptString.getBytes()));
     }
     
     public byte[] doDecrypt(byte[] arg_bteArray) throws Exception 
     {
        objCipher.init(Cipher.DECRYPT_MODE, objKey);     
        return objCipher.doFinal(arg_bteArray);
     }
     //解密字串
     public String decrypt(String arg_strToDecriptString) throws Exception 
     {
         return new String(doDecrypt(hex2Byte(arg_strToDecriptString)));
     }
}
測試

package com.jIAn.crypt;
import java.util.*;
public class Test 
{
    public static void main(String[] args) 
    {
        try 
        {
            String test = "jIAn";
            DES des = new DES("jIAn");//自定義密鑰
            System.out.println("加密前的字符:"+test);
            System.out.println("加密後的字符:"+des.encrypt(test));
            System.out.println("解密後的字符:"+des.decrypt(des.encrypt(test)));            
        } 
        catch (Exception e)
        {
            
            e.printStackTrace();
        }
        
    }
}

2009年2月7日 星期六

發送文件類型及編碼 Header 在IE中變檔案下載

今天被這個Header發送搞了一個小時多
從伺服器發給Browser 一個文件類型及編碼的Header
指令為 header('Content-Type: text/html ; charset=utf-8');
Firefox及Chrome都可以正確運行
一遇上IE就變成檔案下載...
最後才發現在 text/html 之後多了一個空格
將空格移除後,就正常了 ^^"
header('Content-Type: text/html; charset=utf-8');

順道補上其他語言的文件類型及編碼Header設定
Java
resource.setContentType ("text/html; charset=utf-8");
JSP
<%@ page contenttype="text/html; charset=utf-8"%>
.Net
Response.ContentType = "text/html; charset=utf-8";
Asp
<%Response.charset="utf-8"%>

2008年1月27日 星期日

AzDG可逆加密演算法 for Java

花了一個星期的業餘時間,將AzDGCrypt翻成Java版本
相較VB版的AzDGCrypt,Java版提供自訂私匙傳入參數及指定字元集

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.security.*; 
    
public class AzDg
{ 
    private static String strPrivateKey = "0123456789";
    
    public static byte[] encode(byte[] argAbteSource, byte[] argAbteEncryptKey) 
    {
        argAbteEncryptKey = new Md5().getMD5ofStr(new String(argAbteEncryptKey)).toLowerCase().getBytes();
        byte bteCRCLength = 0;  
        byte[] abteReturn = new byte[argAbteSource.length];
        for (int i = 0; i < argAbteSource.length; ++i)
        {
            bteCRCLength = (bteCRCLength > 31) ? 0 : bteCRCLength;
            abteReturn[i] = (byte)(argAbteSource[i] ^ argAbteEncryptKey[bteCRCLength++]);
        }
        return abteReturn;
    }
    
    public static String encrypt(String argStrSource)
    {
      return encrypt(argStrSource, strPrivateKey);
    }
    
    public static String encrypt(String argStrSource, String argStrPrivateKey)
    {
    return encrypt(argStrSource, argStrPrivateKey,"UTF8");
    }

    public static String encrypt(String argStrSource, String argStrPrivateKey, String argStrCharset)
    {
        String strReturn = null;
        try
        {
           strReturn = new String(encrypt(argStrSource.getBytes(argStrCharset), argStrPrivateKey.getBytes(argStrCharset)),argStrCharset);
        }
        catch (UnsupportedEncodingException e)
        {
           e.printStackTrace();
        }
        return strReturn;
    }

    public static byte[] encrypt(byte[] argStrSource, byte[] argBteKey) 
    {          
        byte[] abteEncryptKey = new Md5().getMD5ofStr(Long.toString( Calendar.getInstance().getTimeInMillis())).getBytes();
        byte bteCRCLength = 0;  
        byte[] abteReturn = new byte[argStrSource.length * 2];  
        for (int i = 0, j = 0; i < argStrSource.length; ++i, ++j) 
        {
            bteCRCLength = bteCRCLength > 31 ? 0 : bteCRCLength;  
            abteReturn[j] = abteEncryptKey[bteCRCLength];
            ++j;
            abteReturn[j] = (byte)(argStrSource[i] ^ abteEncryptKey[bteCRCLength++]);
        }        
        return Base64.encode(encode(abteReturn, argBteKey));
    }
     
    public static String decrypt(String argStrSource)
    {
        return decrypt(argStrSource,strPrivateKey);
    }
     
    public static String decrypt(String argStrSource, String argStrPrivateKey)
    {
        return decrypt(argStrSource, argStrPrivateKey, "UTF8");
    }
    
    public static String decrypt(String argStrSource, String argStrPrivateKey, String argStrCharset)
    {
        String strReturn ="";
        try
        {
            strReturn= new String(decrypt(Base64.decode(argStrSource), argStrPrivateKey.getBytes(argStrCharset)),argStrCharset);
        }
        catch (UnsupportedEncodingException e)
        {
           e.printStackTrace();
        }
        return strReturn;
    }

    public static byte[] decrypt(byte[] argStrSource, byte[] argStrPrivateKey) 
    {
        argStrSource = encode(argStrSource, argStrPrivateKey);
        byte[] abteReturn = new byte[(int)(argStrSource.length / 2)]; 
        for (int i = 0, j = 0; i < argStrSource.length; ++i, ++j)
        {
            abteReturn[j] = (byte)(argStrSource[i] ^ argStrSource[++i] );
        }  
        return abteReturn;
    }
    
    
    public static byte[] md5Byte(String d) throws NoSuchAlgorithmException 
    {
        MessageDigest md = MessageDigest.getInstance("MD5");         
        md.update(d.getBytes()); 
        return  md.digest();         
    }        
}

2007年11月11日 星期日

fedora 安裝java

fedora本身就帶有gcj版的java(Fedora8新增icedtea發行版但仍帶有gcj)
但已習慣使用Sun的出版的JDK,所以就將系統替換成Sun出版的Jdk6
首先下載到Sun的Java官網下載最新版的JDK
解開後將資料夾移到/opt/java/jdk1.6.0_03
接著設定Java
先執行alternatives --config java檢查系統中的Java版本及順序
alternatives --install /usr/bin/java java /opt/java/jdk1.6.0_03/jre/bin/java 3
/usr/bin/java java 是固定的
/opt/java/jdk1.6.0_03/jre/bin/java 則是剛剛JDK的路徑
3則是出現選單的順序
接著再執行一次alternatives --config java 選剛剛新增加的3
再來就是替換java的編譯器
alternatives --install /usr/bin/java javac /opt/java/jdk1.6.0_03/bin/javac 3
規則如上,接著設定java編譯器
alternatives --config javac
一樣選剛剛新增加的3
接著看版本是否被替換了
java -version
javac -version