Skip to content

Instantly share code, notes, and snippets.

@johnsonz
johnsonz / go-zip-uncompress-cmd
Created May 17, 2016 08:18
go使用7z解压zip文件
func uncompress(dir string) {
err := filepath.Walk(dir, func(path string, file os.FileInfo, err error) error {
if file == nil {
return nil
}
if file.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext == ".zip" {
@johnsonz
johnsonz / go-zip-uncompress-native
Created May 17, 2016 08:20
go使用archive/zip原生包解压zip文件
package main
import (
"archive/zip"
"fmt"
"io"
"os"
)
func main() {
@johnsonz
johnsonz / go-rar-uncompress-cmd
Created May 17, 2016 08:22
go使用winrar解压rar文件
func uncompress(dir string) {
err := filepath.Walk(dir, func(path string, file os.FileInfo, err error) error {
if file == nil {
return nil
}
if file.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext == ".rar" {
@johnsonz
johnsonz / c#-read-excel-using-NPOI
Created May 19, 2016 07:32
C#使用NPOI操作excel文件
private DataTable GetAllShowMobileFromExcel(string path)
{
DataTable dt = new DataTable();
dt.Columns.Add("Mobile",typeof(string));
DataRow dr = null;
IWorkbook wb = null;
using (FileStream fs = File.OpenRead(path))
{
wb = WorkbookFactory.Create(fs);
@johnsonz
johnsonz / c#-stop-start-IIS-application-pool
Created May 19, 2016 07:39
C#重启IIS Application Pool
private void StopAppPool(string AppPoolName, string method)
{
try
{
//1: Starting
//2: Started
//3: Stopping
//4: Stopped
//5: Pausing
//6: Paused
@johnsonz
johnsonz / c#-send-email
Created May 19, 2016 07:41
C#发送邮件
private void SendEmail(string from, string subject, string body, string[] to, string[] cc, string[] bcc, string host, int port)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from);
if (to.Length > 0)
{
foreach (string s in to)
{
@johnsonz
johnsonz / go-read-file
Created May 20, 2016 08:26
go使用ioutil读取文件
func ReadFile(path string) []byte {
fmt.Println("path=", path)
content, err := ioutil.ReadFile(path)
if err != nil {
glog.Errorf("read file err: %v", err)
}
return content
}
@johnsonz
johnsonz / go-copy-file-and-dir
Last active May 20, 2016 08:36
go文件和目录复制,文件复制是建立硬链接,如失败,则复制文件内容
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func CopyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
@johnsonz
johnsonz / go-parse-json-to-struct
Created May 20, 2016 08:39
go解析JSON文件到struct类型
func ParseConfig() {
currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
glog.Fatalf("get current directory err: %v", err)
}
jsontext, err := ioutil.ReadFile(currentDir + "\\" + configName)
if err != nil {
glog.Fatalf("read config.json file err: %v", err)
}
err = json.Unmarshal(jsontext, &config)
@johnsonz
johnsonz / go-base64-encode-decode
Created May 20, 2016 09:07
go base64 编码和解码
func Base64Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func Base64Decode(b []byte) ([]byte, error) {
return base64.StdEncoding.DecodeString(string(b))
}